/// <summary> /// Converts a number 0 <= n <= 999 to its word representation by appending it to a StringBuilder. /// The flag denotes if the function aborts if num == 0 and therefore does not append anything. /// </summary> /// <param name="flag">Denotes if the function aborts if num == 0.</param> /// <param name="num">Number which gets converted.</param> /// <param name="str">StringBuilder to which result is appended.</param> /// <returns>True, if something was appended.</returns> public static bool AppendToFlag(bool flag, uint num, StringBuilder str) { if (num > 999) { throw new ArgumentOutOfRangeException("num", "Argument must be within 0 and 999"); } // shortcut: if (num == 0 && flag) { return(false); } if (num < 100) { UpToTwoDigits(num, str); return(true); } // 100 <= num <= 999 uint hundreds = num / 100; // this is >= 1 and <= 9 str.Append(Numerals.LowAsString(hundreds)); str.Append(" hundred"); uint tens = num % 100; // add tens if neccessary: if (tens != 0) { str.Append(" "); UpToTwoDigits(tens, str); } return(true); }
/// <summary> /// Converts a number 0 <= n <= 99 to its word representation. /// </summary> private static void UpToTwoDigits(uint num, StringBuilder str) { if (num > 99) { throw new ArgumentOutOfRangeException("num", "Argument must be within 0 and 99"); } // shortcut: if (num <= 19) { str.Append(Numerals.LowAsString(num)); return; } // 20 <= num <= 99 uint tens = num / 10; // this is something >= 2 and <= 9 ---> tens-2 is index in HighNumerals uint ones = num % 10; // this is something between 0 and 9 str.Append(Numerals.HighAsString(tens - 2)); if (ones != 0) // avoid ninety-zero, instead: ninety { str.Append("-"); str.Append(Numerals.LowAsString(ones)); } }