示例#1
0
        public NumberFormatPattern ToDisplayFormat(NumberFormat formatter)
        {
            Guard.ArgumentNotNull(formatter, "formatter");
            int mnd  = formatter.Digits.MinimumIntegerDigits;
            int mnfd = 21;
            int mxfd = 21;

            if (formatter.Digits.RoundingType != RoundingType.SignificantDigits)
            {
                mnfd = formatter.Digits.MinimumFractionDigits;
                mxfd = formatter.Digits.MaximumFractionDigits;
            }
            string key = String.Join(";", new[] { this.PositivePattern.ToString(), this.NegativePattern.ToString(), this.ZeroPattern.ToString(), formatter.SignDisplay.ToString(), formatter.UseGrouping.ToString(), mnd.ToString(), mnfd.ToString(), mxfd.ToString() });

            return(cache.GetOrAdd(key, _ => {
                FormattedString[] patterns = new[] { this.PositivePattern, this.NegativePattern, this.ZeroPattern };
                string negativePattern = this.NegativePattern.First(v => v.Type == FormattedPartType.Placeholder).Value;
                bool[] addPlusSign = new bool[3];
                addPlusSign[0] = formatter.SignDisplay == SignDisplayFormat.Always || formatter.SignDisplay == SignDisplayFormat.ExceptZero;
                addPlusSign[2] = formatter.SignDisplay == SignDisplayFormat.Always;

                for (int i = 0; i < 3; i++)
                {
                    FormattedPart[] parts = patterns[i].GetParts();
                    int index = Array.FindIndex(parts, v => v.Type == FormattedPartType.Placeholder);
                    string pattern = MakePattern(parts[index].Value, negativePattern, addPlusSign[i], formatter.UseGrouping, mnd, mnfd, mxfd);
                    if (pattern != parts[index].Value)
                    {
                        parts[index] = new FormattedPart(FormattedPartType.Placeholder, pattern);
                        patterns[i] = new FormattedString(parts);
                    }
                }
                return new NumberFormatPattern(patterns[0], patterns[1], patterns[2]);
            }));
        }
示例#2
0
        public FormattedString Format(IList <string> items)
        {
            Guard.ArgumentNotNull(items, "items");
            EnsureInitialized();
            if (items.Count == 0)
            {
                return(FormattedString.Empty);
            }
            if (items.Count == 1)
            {
                return(new FormattedString(new[] { new FormattedPart(FormattedPartType.Element, items[0]) }));
            }
            int countMinus2 = items.Count - 2;

            if (patterns == null)
            {
                patterns = ResolvePatterns(this.Locale, ToLdmlListPatternType(this.Type, this.Style));
            }
            if (countMinus2 == 0)
            {
                FormattedPart[] parts = patterns[3].GetParts();
                for (int i = 0, len = parts.Length; i < len; i++)
                {
                    if (parts[i].Type == FormattedPartType.Placeholder)
                    {
                        parts[i] = new FormattedPart(FormattedPartType.Element, parts[i].Value == "{0}" ? items[0] : items[1]);
                    }
                }
                return(new FormattedString(parts));
            }
            List <FormattedPart> list = new List <FormattedPart> {
                new FormattedPart(FormattedPartType.Element, items[countMinus2 + 1])
            };

            for (int j = countMinus2; j >= 0; j--)
            {
                FormattedPart[] parts      = patterns[j == 0 ? 0 : j < countMinus2 ? 1 : 2].GetParts();
                int             sliceIndex = 0;
                for (int i = 0, len = parts.Length; i < len; i++)
                {
                    if (parts[i].Type == FormattedPartType.Placeholder)
                    {
                        if (parts[i].Value == "{0}")
                        {
                            parts[i] = new FormattedPart(FormattedPartType.Element, items[j]);
                        }
                        else
                        {
                            sliceIndex = i;
                        }
                    }
                }
                list.InsertRange(0, parts.Take(sliceIndex));
                list.AddRange(parts.Skip(sliceIndex + 1));
            }
            return(new FormattedString(list));
        }