Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting number to string representation
Message
From
28/04/2009 04:16:04
 
 
To
27/04/2009 16:45:23
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01396542
Message ID:
01396621
Views:
52
>>... for example, 1 to one, 2 to two, etc... Is there a class in .NET to do this? Thanks!
>
>I've found an example elsewhere, and modified a bit to fit my needs. See comments for original source and modifications:
>
>
>        //Wrapper class for NumberToText(int n) to account for single zero parameter.
>        public static string ConvertToStringRepresentation(long number)
>        {
>            string result = null;
>
>            if (number == 0)
>            {
>                result = "Zero";
>            }
>            else
>            {
>                result = NumberToText(number);
>            }
>
>            return result;
>        }
>        
>        //Found at http://www.dotnet2themax.com/blogs/fbalena/PermaLink,guid,cdceca73-08cd-4c15-aef7-0f9c8096e20a.aspx.
>        //Modifications from original source:
>        //  Changed parameter type from int to long.
>        //  Changed labels to be singulars instead of plurals (Billions to Billion, Millions to Million, etc.).
>        private static string NumberToText(long n)
>        {
>            if (n < 0)
>                return "Minus " + NumberToText(-n);
>            else if (n == 0)
>                return "";
>            else if (n <= 19)
>                return new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", 
>                                        "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", 
>                                        "Seventeen", "Eighteen", "Nineteen"}[n - 1] + " ";
>            else if (n <= 99)
>                return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", 
>                                        "Eighty", "Ninety"}[n / 10 - 2] + " " + NumberToText(n % 10);
>            else if (n <= 199)
>                return "One Hundred " + NumberToText(n % 100);
>            else if (n <= 999)
>                return NumberToText(n / 100) + "Hundred " + NumberToText(n % 100);
>            else if (n <= 1999)
>                return "One Thousand " + NumberToText(n % 1000);
>            else if (n <= 999999)
>                return NumberToText(n / 1000) + "Thousand " + NumberToText(n % 1000);
>            else if (n <= 1999999)
>                return "One Million " + NumberToText(n % 1000000);
>            else if (n <= 999999999)
>                return NumberToText(n / 1000000) + "Million " + NumberToText(n % 1000000);
>            else if (n <= 1999999999)
>                return "One Billion " + NumberToText(n % 1000000000);
>            else
>                return NumberToText(n / 1000000000) + "Billion " + NumberToText(n % 1000000000);
>        }
>
Hi,
Neat recursion! I wonder if using a StringBuilder might be quicker (also making the arrays static):
    public static class NTW
    {
        private static readonly string[] s1 ={"One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", 
                                        "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", 
                                        "Seventeen ", "Eighteen ", "Nineteen "};

        private static readonly string[] s2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", 
                                        "Eighty ", "Ninety "};

        //Wrapper class for NumberToText(int n) to account for single zero parameter.
        public static string ConvertToStringRepresentation(long number)
        {
            string result = null;
            if (number == 0)
            {
                result = "Zero";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                NumberToText(number, ref sb);
                result = sb.ToString();
            }
            return result;
        }

        private static void NumberToText(long n, ref StringBuilder sb)
        {
            if (n < 0)
            {
                sb.Append("Minus "); NumberToText(-n, ref sb);
            }
            else if (n <= 19)
                sb.Append(s1[n - 1] );
            else if (n <= 99)
            {
                sb.Append(s2[n / 10 - 2] );
                NumberToText(n % 10, ref sb);
            }
            else if (n <= 199)
            {
                sb.Append("One Hundred ");
                NumberToText(n % 100, ref sb);
            }
            else if (n <= 999)
            {
                NumberToText(n / 100, ref sb);
                sb.Append("Hundred ");
                NumberToText(n % 100, ref sb);
            }
            else if (n <= 1999)
            {
                sb.Append("One Thousand ");
                NumberToText(n % 1000, ref sb);
            }
            else if (n <= 999999)
            {
                NumberToText(n / 1000, ref sb);
                sb.Append("Thousand ");
                NumberToText(n % 1000, ref sb);
            }
            else if (n <= 1999999)
            {
                sb.Append("One Million ");
                NumberToText(n % 1000000, ref sb);
            }
            else if (n <= 999999999)
            {
                NumberToText(n / 1000000, ref sb);
                sb.Append("Million ");
                NumberToText(n % 1000000, ref sb);
            }
            else if (n <= 1999999999)
            {
                sb.Append("One Billion ");
                NumberToText(n % 1000000000, ref sb);
            }
            else
            {
                NumberToText(n / 1000000000, ref sb);
                sb.Append("Billion ");
                NumberToText(n % 1000000000, ref sb);
            }
        }
    }
Also be nice to put an "and" after the "Hundred" where ness.....
Best,
Viv
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform