/// <summary>
        ///   Creates or reuses a number formatter for the specified <see cref="Locale"/>.
        /// </summary>
        /// <param name="locale">
        ///   The locale.
        /// </param>
        /// <param name="options">
        ///   The options to apply when formating a number.
        /// </param>
        /// <returns>
        ///   The formatter that is the best for the <paramref name="locale"/>.
        /// </returns>
        public static INumberFormatter Create(Locale locale, NumberOptions options = null)
        {
            var             numberingSystem = NumberingSystem.Create(locale);
            NumberFormatter formatter;

            if (numberingSystem.IsNumeric)
            {
                formatter = new NumericFormatter
                {
                    Locale          = locale,
                    NumberingSystem = numberingSystem,
                    Symbols         = NumberSymbols.Create(locale),
                    Options         = options ?? new NumberOptions()
                };
            }

            else // Must be Algorithmic
            {
                formatter = new AlgorithmicFormatter
                {
                    Locale          = locale,
                    NumberingSystem = numberingSystem,
                    Options         = options ?? new NumberOptions()
                };
            }

            formatter.Resolve();
            return(formatter);
        }
        public void Create_From_Locale()
        {
            var locale  = Locale.Create("de");
            var symbols = NumberSymbols.Create(locale);

            Assert.AreEqual(",", symbols.CurrencyDecimal);
            Assert.AreEqual(".", symbols.CurrencyGroup);
            Assert.AreEqual(",", symbols.Decimal);
            Assert.AreEqual("E", symbols.Exponential);
            Assert.AreEqual(".", symbols.Group);
            Assert.AreEqual("∞", symbols.Infinity);
            Assert.AreEqual(";", symbols.List);
            Assert.AreEqual("-", symbols.MinusSign);
            Assert.AreEqual("NaN", symbols.NotANumber);
            Assert.AreEqual("%", symbols.PercentSign);
            Assert.AreEqual("‰", symbols.PerMille);
            Assert.AreEqual("+", symbols.PlusSign);
            Assert.AreEqual("·", symbols.SuperscriptingExponent);

            locale  = Locale.Create("de-AT");
            symbols = NumberSymbols.Create(locale);
            Assert.AreEqual(",", symbols.CurrencyDecimal);
            Assert.AreEqual(".", symbols.CurrencyGroup);
            Assert.AreEqual(",", symbols.Decimal);
            Assert.AreEqual("E", symbols.Exponential);
            Assert.AreEqual("\u00A0", symbols.Group);
            Assert.AreEqual("∞", symbols.Infinity);
            Assert.AreEqual(";", symbols.List);
            Assert.AreEqual("-", symbols.MinusSign);
            Assert.AreEqual("NaN", symbols.NotANumber);
            Assert.AreEqual("%", symbols.PercentSign);
            Assert.AreEqual("‰", symbols.PerMille);
            Assert.AreEqual("+", symbols.PlusSign);
            Assert.AreEqual("·", symbols.SuperscriptingExponent);
        }
Exemple #3
0
        /// <summary>
        ///   Creates the symbols for the specified <see cref="Locale"/>.
        /// </summary>
        /// <param name="locale">
        ///   The locale.
        /// </param>
        /// <returns>
        ///   The symbols that are the best for the <paramref name="locale"/>.
        /// </returns>
        public static NumberSymbols Create(Locale locale)
        {
            var ns      = NumberingSystem.Create(locale).Id;
            var path    = $"ldml/numbers/symbols[@numberSystem='{ns}']/";
            var symbols = new NumberSymbols
            {
                Decimal                = locale.Find(path + "decimal").Value,
                Exponential            = locale.Find(path + "exponential").Value,
                Group                  = locale.Find(path + "group").Value,
                Infinity               = locale.Find(path + "infinity").Value,
                List                   = locale.Find(path + "list").Value,
                MinusSign              = locale.Find(path + "minusSign").Value,
                NotANumber             = locale.Find(path + "nan").Value,
                PercentSign            = locale.Find(path + "percentSign").Value,
                PerMille               = locale.Find(path + "perMille").Value,
                PlusSign               = locale.Find(path + "plusSign").Value,
                SuperscriptingExponent = locale.Find(path + "superscriptingExponent").Value
            };

            var found = locale.FindOrDefault(path + "currencyDecimal");

            if (found != null)
            {
                symbols.CurrencyDecimal = found.Value;
            }

            found = locale.FindOrDefault(path + "currencyGroup");
            if (found != null)
            {
                symbols.CurrencyGroup = found.Value;
            }

            return(symbols);
        }
        public void Currency_Defaults()
        {
            var symbols = new NumberSymbols
            {
                Decimal = ".",
                Group   = ","
            };

            Assert.AreEqual(".", symbols.CurrencyDecimal);
            Assert.AreEqual(",", symbols.CurrencyGroup);

            symbols = new NumberSymbols
            {
                CurrencyDecimal = ".X",
                CurrencyGroup   = ",X"
            };
            Assert.AreEqual(".X", symbols.CurrencyDecimal);
            Assert.AreEqual(",X", symbols.CurrencyGroup);
        }