Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Converting number to string representation
Message
De
28/04/2009 08:22:13
 
 
À
28/04/2009 05:03:00
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01396542
Message ID:
01396641
Vues:
53
>>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
>
>Viv,
>
>
>I think that the use of a StringBuilder is a good idea - also making the arrays static
>
>You do not really need to pass sb by reference, do you ?
You're right. Of course not. Just before my first coffee.

>Do you know in which cases one puts an 'and' between Hundred and what follows ?

I'd think any time anything follows. e.g:
1300 = One Thousand Three Hundred
1327 = One Thousand Three Hundred and Twenty Seven
etc
>
>Also, a good candidate for an extension method, I think
I'd think perfect candidate (s)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform