Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Converting number to string representation
Message
De
28/04/2009 09:36:37
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
 
 
À
28/04/2009 09:18:42
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01396542
Message ID:
01396664
Vues:
50
Looks familiar...

>See also http://stackoverflow.com/questions/794663/net-convert-number-to-string-representation-1-to-one-2-to-two-etc ;)
>
>>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
Répondre
Fil
Voir

Click here to load this message in the networking platform