Esempio n. 1
0
        public void Format_Where_NumberStringWithSpecialChars_Expected_InvalidOperation()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("!@$%*&*(^(^))((.{}:?<>", enRoundingType.None, 0, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();

            dev2NumberFormatter.Format(formatNumberTO);
        }
        // ReSharper disable InconsistentNaming
        private string BuildRoundingExpression(FormatNumberTO formatNumberTO)
        // ReSharper restore InconsistentNaming
        {
            string expression;

            enRoundingType roundingType = formatNumberTO.GetRoundingTypeEnum();

            if (roundingType == enRoundingType.Normal)
            {
                expression = "round({0}, {1})";
            }
            else if (roundingType == enRoundingType.Up)
            {
                expression = "roundup({0}, {1})";
            }
            else if (roundingType == enRoundingType.Down)
            {
                expression = "rounddown({0}, {1})";
            }
            else
            {
                expression = "{0}";
            }

            expression = string.Format(expression, formatNumberTO.Number, formatNumberTO.RoundingDecimalPlaces);

            return(expression);
        }
Esempio n. 3
0
        public void Format_Where_DecimalPlacesToShowGreaterThanNegative14_Expected_InvalidOperation()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("123.123", enRoundingType.None, 0, true, -15);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();

            dev2NumberFormatter.Format(formatNumberTO);
        }
        // ReSharper restore InconsistentNaming

        #endregion Class Members

        #region Methods

        /// <summary>
        /// Formats a number.
        /// </summary>
        /// <param name="formatNumberTO">The information on how to format the number.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">formatNumberTO</exception>
        // ReSharper disable InconsistentNaming
        public string Format(FormatNumberTO formatNumberTO)
        // ReSharper restore InconsistentNaming
        {
            if (formatNumberTO == null)
            {
                throw new ArgumentNullException("formatNumberTO");
            }

            decimal tmp;

            if (!formatNumberTO.Number.IsNumeric(out tmp))
            {
                throw new InvalidOperationException("Unable to format '" + formatNumberTO.Number + "' because it isn't a number.");
            }

            if (formatNumberTO.RoundingDecimalPlaces < -14 || formatNumberTO.RoundingDecimalPlaces > 14)
            {
                throw new InvalidOperationException("Rounding decimal places must be between -14 and 14.");
            }

            if (formatNumberTO.AdjustDecimalPlaces && (formatNumberTO.DecimalPlacesToShow < -14 || formatNumberTO.DecimalPlacesToShow > 14))
            {
                throw new InvalidOperationException("Decimal places to show must be less between -14 than 14.");
            }

            string result = Round(formatNumberTO);

            result = AdjustDecimalPlaces(result, formatNumberTO.AdjustDecimalPlaces, formatNumberTO.DecimalPlacesToShow);
            return(result);
        }
Esempio n. 5
0
        public void Format_Where_RoundingDecimalPlacesLessThanNegative14_Expected_InvalidOperation()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("123.123", enRoundingType.None, -15, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();

            dev2NumberFormatter.Format(formatNumberTO);
        }
Esempio n. 6
0
        public void Format_Where_NumberStringIsntANumber_Expected_InvalidOperation()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("ABC", enRoundingType.None, 0, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();

            dev2NumberFormatter.Format(formatNumberTO);
        }
Esempio n. 7
0
        public void Format_Where_RoundingDecimalPlacesGreaterThan14_Expected_InvalidOperation()
        {
            var formatNumberTO      = new FormatNumberTO("123.123", enRoundingType.None, 15, false, 0);
            var dev2NumberFormatter = new Dev2NumberFormatter();

            dev2NumberFormatter.Format(formatNumberTO);
        }
        public void Format_Given_NegativeNumber_Where_RoundingTypeIsNoneAndAdjustDecimalsIsFalse_Expected_RawNumberBack()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.123456", enRoundingType.None, 0, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string actual = dev2NumberFormatter.Format(formatNumberTO);

            Assert.AreEqual(formatNumberTO.Number, actual);
        }
Esempio n. 9
0
        public void Format_Given_NegativeNumberNumberWithMoreThan2DecimalPlaces_Where_AdjustDecimalPlacesBy2_Expected_2DecimalPlaces()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.12345678911235", enRoundingType.None, 0, true, 2);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -123.12d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 10
0
        public void Format_Given_PositiveNumber_Where_AdjustDecimalPlacesBy0_Expected_DecimalsAreDropped()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("123.12345678911235", enRoundingType.None, 0, true, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 123d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 11
0
        public void Format_Given_0_Where_RoundingDecimalPlacesAreNegative_Expected_0()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("0", enRoundingType.Normal, -1, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 0d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 12
0
        public void Format_Given_PositiveNumber_Where_RoundingDecimalPlacesAreNegative_Expected_RoundingToMultiplesOf10ForEveryNegative()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("123.12345", enRoundingType.Normal, -1, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 120d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
        public void Format_Given_0_Where_RoundingTypeIsDown_Expected_0()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("0", enRoundingType.Down, 0, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string actual         = dev2NumberFormatter.Format(formatNumberTO);
            double expectedDouble = 0d;
            string expected       = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
Esempio n. 14
0
        public void Format_Given_NegativeNumberGreaterThanNegative1000_Where_AdjustDecimalPlacesByNegative3_Expected_0()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-1.12345678911235", enRoundingType.None, 0, true, -3);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 0d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 15
0
        public void Format_Given_0_Where_RoundingTypeIsDown_Expected_0()
        {
            var          formatNumberTO      = new FormatNumberTO("0", enRoundingType.Down, 0, false, 0);
            var          dev2NumberFormatter = new Dev2NumberFormatter();
            var          actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 0d;
            var          expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 16
0
        public void Format_Given_NegativeNumber_Where_RoundingTypeIsUp_Expected_RoundedUp()
        {
            var          formatNumberTO      = new FormatNumberTO("-123.92345", enRoundingType.Up, 0, false, 0);
            var          dev2NumberFormatter = new Dev2NumberFormatter();
            var          actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -124d;
            var          expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 17
0
        public void Format_Given_PositiveNumberWithDecimalsThatShouldRoundUp_Where_RoundingTypeIsNormalWithNoDecimalPlaces_Expected_RoundedUp()
        {
            var          formatNumberTO      = new FormatNumberTO("123.92345", enRoundingType.Normal, 0, false, 0);
            var          dev2NumberFormatter = new Dev2NumberFormatter();
            var          actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 124d;
            var          expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 18
0
        public void Format_Given_PositiveNumber_Where_RoundingTypeIsNoneAndAdjustDecimalsIsFalse_Expected_RawNumberBack()
        {
            var formatNumberTO      = new FormatNumberTO("123.123456", enRoundingType.None, 0, false, 0);
            var dev2NumberFormatter = new Dev2NumberFormatter();
            var actual = dev2NumberFormatter.Format(formatNumberTO);

            actual = "'" + actual + "'";

            Assert.AreEqual(formatNumberTO.Number, actual);
        }
Esempio n. 19
0
        public void Format_Given_NegativeNumberWithLessThanThan5DecimalPlaces_Where_AdjustDecimalPlacesBy5_Expected_2DecimalPlaces()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.123", enRoundingType.None, 0, true, 5);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -123.12300d;
            string       expected       = expectedDouble.ToString("###.###00");

            Assert.AreEqual(expected, actual);
        }
Esempio n. 20
0
        public void Format_Given_NegativeNumber_Where_RoundingDecimalPlacesArelessThanWhatIsBeingAdjusted_Expected_RoundingHappensThenDecimalsAreAdded()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.12395678911235", enRoundingType.Normal, 3, true, 4);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -123.124d;
            string       expected       = expectedDouble.ToString("###.###0");

            Assert.AreEqual(expected, actual);
        }
Esempio n. 21
0
        public void Format_Given_NegativeNumber_Where_AdjustDecimalPlacesByNegative1_Expected_AnyValuAboveNagative10IsDropped()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.12345678911235", enRoundingType.None, 0, true, -1);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -12d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
        public void Format_Given_PositiveNumberLessThan1000_Where_AdjustDecimalPlacesByNegative3_Expected_0()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("1.12345678911235", enRoundingType.None, 0, true, -3);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string actual         = dev2NumberFormatter.Format(formatNumberTO);
            double expectedDouble = 0d;
            string expected       = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
Esempio n. 23
0
        public void Format_Given_NegativeNumber_Where_RoundingDecimalPlacesAreMoreThanWhatIsBeingAdjusted_Expected_RoundingHappensThenDecimalsAreDropped()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.12395678911235", enRoundingType.Normal, 4, true, 3);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -123.124d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 24
0
        public void Format_Given_NegativeNumberWithDecimalsThatShouldRoundUp_Where_RoundingTypeIsNormalWithDecimalPlaces_Expected_RoundedUp()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.12645", enRoundingType.Normal, 2, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -123.13d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 25
0
        public void Format_Given_0_Where_RoundingDecimalPlacesAreLessThanWhatIsBeingAdjusted_Expected_0()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("0", enRoundingType.Normal, 3, true, 4);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 0d;
            string       expected       = expectedDouble.ToString("0.0000");

            Assert.AreEqual(expected, actual);
        }
        public void Format_Given_NegativeNumber_Where_RoundingTypeIsUp_Expected_RoundedUp()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.92345", enRoundingType.Up, 0, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string actual         = dev2NumberFormatter.Format(formatNumberTO);
            double expectedDouble = -124d;
            string expected       = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
Esempio n. 27
0
        public void Format_Given_PositiveNumber_Where_RoundingTypeIsUpWithDecimalPlaces_Expected_RoundedUp()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("123.12345", enRoundingType.Up, 2, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = 123.13d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
        public void Format_Given_NegativeNumber_Where_RoundingTypeIsDownWithDecimalPlaces_Expected_RoundedDown()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.12345", enRoundingType.Down, 2, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string actual         = dev2NumberFormatter.Format(formatNumberTO);
            double expectedDouble = -123.12d;
            string expected       = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
Esempio n. 29
0
        public void Format_Given_NegativeNumber_Where_RoundingTypeIsDownWithNoDecimalPlaces_Expected_RoundedDown()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("-123.92345", enRoundingType.Down, 0, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string       actual         = dev2NumberFormatter.Format(formatNumberTO);
            const double expectedDouble = -123d;
            string       expected       = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
        public void Format_Given_NegativeNumberWithDecimalsThatShouldRoundUp_Where_RoundingTypeIsNormalWithNoDecimalPlaces_Expected_RoundedUp()
        {
            FormatNumberTO      formatNumberTO      = new FormatNumberTO("123.92345", enRoundingType.Normal, 0, false, 0);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string actual         = dev2NumberFormatter.Format(formatNumberTO);
            double expectedDouble = 124;
            string expected       = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }