Convert Amount to Words
100.01 = One Hundred Dollar And One Cent
101 = One Hundred And One Dollar
101.01 = One Hundred And One Dollar And One Cent
0.1 = Ten Cent
1.1 = One Dollar And Ten Cent
1,000,000 =One Million Dollar
public static class AmountToWords { private static string[] ones = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private static string[] tens = { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; private static string[] thous = { "Hundred", "Thousand", "Million", "Billion", "Trillion", "Quadrillion" }; private static string fmt_negative = "Negative {0}"; private static string fmt_dollars = "{0} Dollar"; private static string fmt_cents = "{0} Cent"; private static string fmt_dollars_and_cents = "{0} Dollar {1} Cent"; private static string fmt_tens_ones = "{0} {1}"; // e.g. for twenty-one, thirty-two etc. You might want to use an en-dash or em-dash instead of a hyphen. private static string fmt_large_small = "{0} {1}"; // stitches together the large and small part of a number, like "{three thousand} {five hundred forty two}" private static string fmt_amount_scale = "{0} {1}"; // adds the scale to the number, e.g. "{three} {million}"; private static string fmt_and = "And "; public static string ToWords(decimal number) { if (number < 0) return string.Format(fmt_negative, ToWords(Math.Abs(number))); string result = string.Empty; long longPortion = (long)number; long decPortion = (long)((number - longPortion) * (decimal)100); if (number == 0) result = string.Format(fmt_dollars, ones[0]); else if (decPortion == 0) result = string.Format(fmt_dollars, ToWords(longPortion)); else if (number < 1) result = string.Format(fmt_cents, ToWords(decPortion)); else result = string.Format(fmt_dollars_and_cents, ToWords(longPortion), ToWords(decPortion)); if (result.IndexOf(fmt_and) == 0) { result = result.Substring(result.IndexOf(fmt_and) + fmt_and.Length); } return result; } private static string ToWords(long number, string appendScale = "") { string numString = ""; if (number == 0) { //hide Zero } // if the number is less than one hundred, then we're mostly just pulling out constants from the ones and tens dictionaries else if (number < 100) { if (number < 20) numString = ones[number]; else { numString = tens[number / 10]; if ((number % 10) > 0) numString = string.Format(fmt_tens_ones, numString, ones[number % 10]); } if (string.IsNullOrEmpty(appendScale)) numString = fmt_and + numString; } else { long pow = 0; // we'll divide the number by pow to figure out the next chunk string powStr = ""; // powStr will be the scale that we append to the string e.g. "hundred", "thousand", etc. if (number < 1000) { // number is between 100 and 1000 pow = 100; // so we'll be dividing by one hundred powStr = thous[0]; // and appending the string "hundred" } else { // find the scale of the number // log will be 1, 2, 3 for 1_000, 1_000_000, 1_000_000_000, etc. long log = (long)Math.Log(number, 1000); // pow will be 1_000, 1_000_000, 1_000_000_000 etc. pow = (long)Math.Pow(1000, log); // powStr will be thousand, million, billion etc. powStr = thous[log]; } // we take the quotient and the remainder after dividing by pow, and call ToWords on each to handle cases like "{five thousand} {thirty two}" (curly brackets added for emphasis) numString = string.Format(fmt_large_small, ToWords(number / pow, powStr), ToWords(number % pow)).Trim(); } // and after all of this, if we were passed in a scale from above, we append it to the current number "{five} {thousand}" return string.Format(fmt_amount_scale, numString, appendScale).Trim(); } }