Пример #1
0
        public CurrencyFormat(
            int decimalDigits             = 2,
            string decimalSeparator       = ".",
            string groupSeparator         = ",",
            IEnumerable <int> groupSizes  = null,
            string currencySymbol         = "",
            PositiveFormat positiveFormat = PositiveFormat.Prefix,
            NegativeFormat negativeFormat = NegativeFormat.Parentheses)
        {
            if (decimalDigits < 0 || decimalDigits > 99)
            {
                throw new ArgumentOutOfRangeException(nameof(decimalDigits));
            }

            if (string.IsNullOrEmpty(decimalSeparator))
            {
                throw new ArgumentNullException(nameof(decimalSeparator));
            }

            if (string.IsNullOrEmpty(groupSeparator))
            {
                throw new ArgumentNullException(nameof(groupSeparator));
            }

            var copy = (groupSizes ?? new[] { 3 }).ToArray();

            for (var i = 0; i < copy.Length; i++)
            {
                var v = copy[i];
                if (v < 0 || (v == 0 && i != copy.Length - 1))
                {
                    throw new ArgumentOutOfRangeException(nameof(groupSizes));
                }
            }

            if (!Enum.GetValues(typeof(PositiveFormat)).Cast <PositiveFormat>().Contains(positiveFormat))
            {
                throw new ArgumentOutOfRangeException(nameof(positiveFormat));
            }

            if (!Enum.GetValues(typeof(NegativeFormat)).Cast <NegativeFormat>().Contains(negativeFormat))
            {
                throw new ArgumentOutOfRangeException(nameof(negativeFormat));
            }

            this.DecimalDigits    = decimalDigits;
            this.DecimalSeparator = decimalSeparator;
            this.groupSeparator   = groupSeparator;
            this.groupSizes       = copy;
            this.PositiveFormat   = positiveFormat;
            this.NegativeFormat   = negativeFormat;
            this.Symbol           = currencySymbol ?? throw new ArgumentNullException(nameof(currencySymbol));

            this.numberFormatInfo = new NumberFormatInfo
            {
                CurrencyDecimalDigits    = this.DecimalDigits,
                CurrencyDecimalSeparator = this.DecimalSeparator,
                CurrencyGroupSeparator   = this.groupSeparator,
                CurrencyGroupSizes       = this.groupSizes,
                CurrencyPositivePattern  = PositivePatternMapping[this.PositiveFormat],
                CurrencyNegativePattern  = NegativePatternMapping[Tuple.Create(this.PositiveFormat, this.NegativeFormat)],
                CurrencySymbol           = this.Symbol,

                NumberDecimalDigits    = this.DecimalDigits,
                NumberDecimalSeparator = this.DecimalSeparator,
                NumberGroupSeparator   = this.groupSeparator,
                NumberGroupSizes       = this.groupSizes,
            };
        }
Пример #2
0
        public CurrencyFormat(
            int decimalDigits = 2,
            string decimalSeparator = ".",
            string groupSeparator = ",",
            IEnumerable<int> groupSizes = null,
            string currencySymbol = "",
            PositiveFormat positiveFormat = PositiveFormat.Prefix,
            NegativeFormat negativeFormat = NegativeFormat.Parentheses)
        {
            if (decimalDigits < 0 || decimalDigits > 99)
            {
                throw new ArgumentOutOfRangeException("decimalDigits");
            }

            if (string.IsNullOrEmpty(decimalSeparator))
            {
                throw new ArgumentNullException("decimalSeparator");
            }

            if (string.IsNullOrEmpty(groupSeparator))
            {
                throw new ArgumentNullException("groupSeparator");
            }

            var copy = (groupSizes ?? new[] { 3 }).ToArray();
            for (int i = 0; i < copy.Length; i++)
            {
                var v = copy[i];
                if (v < 0 || (v == 0 && i != copy.Length - 1))
                {
                    throw new ArgumentOutOfRangeException("groupSizes");
                }
            }

            if (currencySymbol == null)
            {
                throw new ArgumentNullException("symbol");
            }

            if (!Enum.GetValues(typeof(PositiveFormat)).Cast<PositiveFormat>().Contains(positiveFormat))
            {
                throw new ArgumentOutOfRangeException("positiveFormat");
            }

            if (!Enum.GetValues(typeof(NegativeFormat)).Cast<NegativeFormat>().Contains(negativeFormat))
            {
                throw new ArgumentOutOfRangeException("negativeFormat");
            }

            this.decimalDigits = decimalDigits;
            this.decimalSeparator = decimalSeparator;
            this.groupSeparator = groupSeparator;
            this.groupSizes = copy;
            this.positiveFormat = positiveFormat;
            this.negativeFormat = negativeFormat;
            this.currencySymbol = currencySymbol;

            this.numberFormatInfo = new NumberFormatInfo
            {
                CurrencyDecimalDigits = this.decimalDigits,
                CurrencyDecimalSeparator = this.decimalSeparator,
                CurrencyGroupSeparator = this.groupSeparator,
                CurrencyGroupSizes = this.groupSizes,
                CurrencyPositivePattern = positivePatternMapping[this.positiveFormat],
                CurrencyNegativePattern = negativePatternMapping[Tuple.Create(this.positiveFormat, this.negativeFormat)],
                CurrencySymbol = this.currencySymbol,

                NumberDecimalDigits = this.DecimalDigits,
                NumberDecimalSeparator = this.decimalSeparator,
                NumberGroupSeparator = this.groupSeparator,
                NumberGroupSizes = this.groupSizes,
            };
        }