// 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;
        }
        // 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;
        }
        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);
            double expectedDouble = 0d;
            string expected = expectedDouble.ToString();

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

            Assert.AreEqual(expected, actual);
        }
 public void Format_Where_NumberStringIsntANumber_Expected_InvalidOperation()
 {
     FormatNumberTO formatNumberTO = new FormatNumberTO("ABC", enRoundingType.None, 0, false, 0);
     Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
     dev2NumberFormatter.Format(formatNumberTO);
 }
        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);
            double expectedDouble = -123.124d;
            string expected = expectedDouble.ToString("###.###0");

            Assert.AreEqual(expected, actual);
        }
        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);
            double expectedDouble = 0d;
            string expected = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
        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);
            double expectedDouble = -123.12300d;
            string expected = expectedDouble.ToString("###.###00");

            Assert.AreEqual(expected, actual);
        }
 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);
 }
 public void Format_Where_NumberStringWithSpecialChars_Expected_InvalidOperation()
 {
     FormatNumberTO formatNumberTO = new FormatNumberTO("!@$%*&*(^(^))((.{}:?<>", enRoundingType.None, 0, false, 0);
     Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
     dev2NumberFormatter.Format(formatNumberTO);
 }
Esempio n. 11
0
        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);
            const double expectedDouble = 0d;
            string expected = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 12
0
        public void Format_Given_PositiveNumber_Where_AdjustDecimalPlacesByNegative1_Expected_AnyValuBelow10IsDropped()
        {
            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);
        }
Esempio n. 13
0
        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);
            const double expectedDouble = 0d;
            string expected = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 14
0
        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);
            const double expectedDouble = -123.12d;
            string expected = expectedDouble.ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(expected, actual);
        }
        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);
            double expectedDouble = 123d;
            string expected = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
        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);
            double expectedDouble = -123.12d;
            string expected = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
 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);
 }
        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);
            double expectedDouble = -12d;
            string expected = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
        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);
        }
        public void Format_Given_PositiveNumber_Where_RoundingDecimalPlacesAreMoreThanWhatIsBeingAdjusted_Expected_RoundingHappensThenDecimalsAreDropped()
        {
            FormatNumberTO formatNumberTO = new FormatNumberTO("123.12395678911235", enRoundingType.Normal, 4, true, 3);
            Dev2NumberFormatter dev2NumberFormatter = new Dev2NumberFormatter();
            string actual = dev2NumberFormatter.Format(formatNumberTO);
            double expectedDouble = 123.124d;
            string expected = expectedDouble.ToString();

            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);
        }
        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);
            double expectedDouble = 0d;
            string expected = expectedDouble.ToString("0.0000");

            Assert.AreEqual(expected, actual);
        }
        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);
            double expectedDouble = 123.13d;
            string expected = expectedDouble.ToString();

            Assert.AreEqual(expected, actual);
        }
        // ReSharper disable InconsistentNaming
        private string Round(FormatNumberTO formatNumberTO)
        // ReSharper restore InconsistentNaming
        {
            string error;
            string result;
            _functionEvaluator.TryEvaluateFunction(BuildRoundingExpression(formatNumberTO), out result, out error);

            if(!string.IsNullOrWhiteSpace(error))
            {
                throw new InvalidOperationException(error);
            }

            return result;
        }
        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);
            double expectedDouble = -123d;
            string expected = expectedDouble.ToString();

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

            Assert.AreEqual(expected, actual);
        }
Esempio n. 27
0
        public void Format_Given_PositiveNumberWithDecimalsThatShouldRoundUp_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);
        }