using System; using System.Collections.Generic; using System.Text; namespace Spell { public class SpellNumbers { private static string[] r_positionArray = { "", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion" }; private static string[] r_digitArray = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; private static string[] r_tensArray = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; private static string[] r_teensArray = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; public static string SpellNum(long num) { bool negative = false; if (num == 0) { return "zero"; } if (num < 0) { num = -num; negative = true; } StringBuilder sb = new StringBuilder(); int pos = 0; while (num > 0) { if (num % 1000 != 0) { sb.Insert(0, SpellThreeDigits((int)(num % 1000)) + r_positionArray[pos]); } if (num / 1000 != 0) { sb.Insert(0, ", "); } num /= 1000; pos++; } if (negative) { sb.Insert(0, "negative "); } return sb.ToString(); } private static string SpellThreeDigits(int threeDigits) { StringBuilder sb = new StringBuilder(); if (threeDigits / 100 != 0) { sb.Append(r_digitArray[threeDigits / 100] + " hundred"); if (threeDigits % 100 != 0) { sb.Append(" "); } } int tensDigit = threeDigits % 100 / 10; int onesDigit = threeDigits % 10; if (tensDigit == 1) { sb.Append(r_teensArray[onesDigit]); } else { sb.Append(r_tensArray[tensDigit]); if (tensDigit != 0 && onesDigit != 0) { sb.Append(" "); } sb.Append(r_digitArray[onesDigit]); } return sb.ToString(); } } }