Alex Rivera | Logout

.NET convert number to string representation (1 to one, 2 to two, etc...)

Asked 2009-04-27T18:24:26.980
31

Is there a built in method in .NET to convert a number to the string representation of the number? For example, 1 becomes one, 2 becomes two, etc.

Edit
Report

1 Answer

17

Ah, there may not be a class to do this, but there was a code golf question which I provided a C# example for:

Code Golf: Number to Words

However, it's not the easiest to read and it only goes up to decimal.MaxValue, so I've written a new version that will go as high as you need to.

I couldn't find any information regarding values higher than vigintillions, but if you append the values to the thou[] array, you can continue going up as far as you like. It still doesn't support fractions, but I'm thinking about adding that at some point.

    static string NumericStringToWords(string NumericValue)
    {
        if ("0" == NumericValue) return "zero";

        string[] units = { "one", "two", "three", "four", "five", 
                           "six", "seven", "eight", "nine" };

        string[] teens = { "eleven", "twelve", "thirteen", "four", "fifteen", 
                           "sixteen", "seventeen", "eighteen", "nineteen" };

        string[] tens = { "ten", "twenty", "thirty", "forty", "fifty", 
                          "sixty", "seventy", "eighty", "ninety" };

        string[] thou = { "thousand", "million", "billion", "trillion", 
                          "quadrillion", "quintillion", "sextillion", 
                          "septillion", "octillion", "nonillion", "decillion", 
                          "udecillion", "duodecillion", "tredecillion", 
                          "quattuordecillion", "quindecillion", "sexdecillion", 
                          "septendecillion", "octodecillion", "novemdecillion", 
                          "vigintillion" };

        string sign = String.Empty;
        if ("-" == NumericValue.Substring(0, 1))
        {
            sign = "minus ";
            NumericValue = NumericValue.Substring(1);
        }

        int maxLen = thou.Length * 3;
        int actLen = NumericValue.Length;
 
answered 2009-04-27T18:28:52.837

Your Answer