Ejemplo n.º 1
0
        public void GetWholeNumber_Should_Handle_Default_DecimalPlaces_To_2(decimal value, decimal expectedWholeNumber, decimal expectedDecimalNumber)
        {
            decimal actualDecimalNumber;
            var     actualWholeNumber = NumberParser.GetWholeNumber(value, out actualDecimalNumber);

            actualWholeNumber.Should().Be(expectedWholeNumber);
            actualDecimalNumber.Should().Be(expectedDecimalNumber);
        }
Ejemplo n.º 2
0
        public void GetWholeNumber_Should_Separate_Whole_And_Fractional_Component(decimal value, int decimalPlaces, decimal expectedWholeNumber, decimal expectedDecimalNumber)
        {
            decimal actualDecimalNumber;
            var     actualWholeNumber = NumberParser.GetWholeNumber(value, out actualDecimalNumber, decimalPlaces);

            actualWholeNumber.Should().Be(expectedWholeNumber);
            actualDecimalNumber.Should().Be(expectedDecimalNumber);
        }
Ejemplo n.º 3
0
        public string Convert(decimal num)
        {
            var wholeNumber = NumberParser.GetWholeNumber(num, out var remainder);
            // parse numbers into usable format
            var groups = GetGroups(wholeNumber);

            var dollarString = string.Empty;

            if (wholeNumber > 0)
            {
                dollarString = wholeNumber > 1 ? "dollars" : "dollar";
            }
            var wholeNumberString = GetString(groups);

            var centsString     = remainder == 1 ? "cent" : "cents";
            var andString       = wholeNumber > 0 ? "and" : string.Empty;
            var remainderGroup  = GetGroups(remainder, includeZero: true);
            var remainderString = GetString(remainderGroup, includeZero: true);

            remainderString = $"{andString} {remainderString}".Trim();

            return($"{wholeNumberString} {dollarString} {remainderString} {centsString}".Trim());
        }
Ejemplo n.º 4
0
        public decimal Validate(string num)
        {
            if (!decimal.TryParse(num, out var number))
            {
                throw new ArgumentException("Invalid argument value (expected decimal).");
            }
            if (number <= 0)
            {
                throw new ArgumentException("Invalid argument value (expected positive number).");
            }
            if (number > 2000000000)
            {
                throw new ArgumentException("Invalid argument value (expected less than 2 billion).");
            }

            NumberParser.GetWholeNumber(number, out var remainder);
            if (remainder % 1 > 0)
            {
                throw new ArgumentException("Invalid argument value (expected at most two decimal places).");
            }

            return(number);
        }