コード例 #1
0
ファイル: Algorithm.cs プロジェクト: kashiash/NumToWords
        /// <summary>
        /// Format integer number with unit of measure to the correct string on russian language.
        /// </summary>
        /// <example>
        /// For arguments (123, UnitOfMeasure.Ruble) method should return "сто двадцать три рубля".
        /// </example>
        public string Convert(long number, UnitOfMeasure unit)
        {
            // Check for the min value that can't be represented as long positive value
            if (number == long.MinValue)
            {
                throw new ArgumentOutOfRangeException(nameof(number));
            }

            _result = new StringBuilder();

            // Check for negative number
            if (number < 0)
            {
                //_result.Append(' ');
                _result.Append(Constants.Minus);
                number = -number;
            }
            else if (number == 0)
            {
                _result.Append(Constants.Zero);
            }

            // Numbers more than 999 billions is not supported
            if (number >= E12)
            {
                throw new ArgumentOutOfRangeException(nameof(number));
            }

            // Write billions
            if (number >= E9)
            {
                var value = number / E9;
                Append((int)value, Constants.Е9Unit);
                number = number % E9;
            }

            // Write millions
            if (number >= E6)
            {
                var value = number / E6;
                Append((int)value, Constants.Е6Unit);
                number = number % E6;
            }

            // Write thouthands
            if (number >= E3)
            {
                var value = number / E3;
                Append((int)value, Constants.Е3Unit);
                number = number % E3;
            }

            // Write hundreds
            if (number > 0)
            {
                Append((int)number, unit);
            }
            else
            {
                AppendUnitOfMeasure(5, unit);
            }

            // Finilize result
            return(_result.ToString().Trim());
        }
コード例 #2
0
 /// <summary>
 /// Convert number to words on russian language (with unit of measure).
 /// </summary>
 /// <example>
 /// <code>
 /// var numInWords = RussianConverter.Format(123, UnitOfMeasure.Ruble);
 /// Console.WriteLine(numInWords);  // Print "сто двадцать три рубля"
 /// </code>
 /// </example>
 /// <param name="number">Number to format.</param>
 /// <param name="unit">Unit of measure.</param>
 /// <returns>Number in words on russian language with unit of measure.</returns>
 public static string Format(long number, UnitOfMeasure unit)
 {
     return(new Algorithm()
            .Convert(number, unit));
 }