Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Converting number to string representation
Message
De
27/04/2009 16:45:23
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
 
 
À
27/04/2009 14:29:00
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01396542
Message ID:
01396584
Vues:
106
This message has been marked as the solution to the initial question of the thread.
>... 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);
        }
Very fitting: http://xkcd.com/386/
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform