Click here to Skip to main content
Click here to Skip to main content

Converting numbers to the word equivalent.

By , 19 Aug 2010
 
I'm sorry, but that's a horrible way to do it...
 
Here is what I would do...
    public class NumberToWords
    {
 
        public static string ConvertToWords(int Number)
        {
            //Split the string into 3 part pieces
            string numStr = Number.ToString();
            List<string> numParts = new List<string>();
 
            string currentStr = "";
 
            int t = 0;
            for (int i = numStr.Length - 1; i >= 0; i--)
            {
                t++;
                currentStr = numStr[i] + currentStr;
 
                if (t == 3)
                {
                    currentStr = "," + currentStr;
                    t = 0;
                }
            }
 
            if (currentStr.StartsWith(","))
                currentStr = currentStr.Remove(0, 1);
 
            string[] vals = currentStr.Split(',');
 
            numParts.AddRange(vals);
 
            List<string> outString = new List<string>();
 
            List<string> delims = new List<string>();
 
            delims.Add("");
 
            if (numParts.Count >= 2)
                delims.Add("Thousand");
 
            if (numParts.Count >= 3)
                delims.Add("Million");
 
            if (numParts.Count >= 4)
                delims.Add("Billion");
 
            if (numParts.Count >= 5)
                delims.Add("Trillion");
 
            int j = delims.Count - 1;
 
            for (int i = 0; i < numParts.Count; i++)
            {
                int num = int.Parse(numParts[i]);
 
                string temp = "";
 

                if (num >= 100)
                    temp = ConvertThreeDigits(num);
                else if (num >= 10)
                    temp = ConvertTwoDigits(num);
                else
                {
                    if (i == 0)
                    {
                        temp = ConvertOneDigit(num);
                    }
                }
 
 
                temp += " " + delims[j];
                j--;
 
                outString.Add(temp);
            }
 
            string retString = string.Join(" ", outString.ToArray());
 
            return retString;
        }
 
        private static string ConvertThreeDigits(int Number)
        {
            int firstDigit = Number / 100;
            int lastDigits = Number - (firstDigit * 100);
 
            if (lastDigits == 0)
                return ConvertOneDigit(firstDigit) + " Hundred";
            else if (lastDigits < 9)
                return ConvertOneDigit(firstDigit) + " Hundred " + ConvertOneDigit(lastDigits);
            else
                return ConvertOneDigit(firstDigit) + " Hundred " + ConvertTwoDigits(lastDigits);
        }
 
        private static string ConvertTwoDigits(int Number)
        {
            int firstDigit = Number / 10;
            int secondDigit = Number - (firstDigit * 10);
 
            if (Number >= 10 && Number < 20)
            {
                if (secondDigit == 4 || secondDigit == 6 ||
                    secondDigit >= 7)
                {
                    return ConvertOneDigit(secondDigit) + "teen";
                }
                else
                {
                    switch (secondDigit)
                    {
                        case 0:
                            return "Ten";
                        case 1:
                            return "Eleven";
                        case 2:
                            return "Twelve";
                        case 3:
                            return "Thirteen";
                        case 5:
                            return "Fifteen";
                        default:
                            return "ERROR";
                    }
                }
            }
            else
            {
                string firstPart = "";
 
                switch (firstDigit)
                {
                    case 2:
                        firstPart = "Twenty";
                        break;
                    case 3:
                        firstPart = "Thirty";
                        break;
                    case 4:
                        firstPart = "Fourty";
                        break;
                    case 5:
                        firstPart = "Fifty";
                        break;
                    case 6:
                        firstPart = "Sixty";
                        break;
                    case 7:
                        firstPart = "Seventy";
                        break;
                    case 8:
                        firstPart = "Eighty";
                        break;
                    case 9:
                        firstPart = "Ninty";
                        break;
                    default:
                        return "ERROR";
                }
 
                if (secondDigit > 0)
                    return firstPart + "-" + ConvertOneDigit(secondDigit);
                else
                    return firstPart;
            }
        }
 
        private static string ConvertOneDigit(int Number)
        {
            switch (Number)
            {
                case 0:
                    return "Zero";
                case 1:
                    return "One";
                case 2:
                    return "Two";
                case 3:
                    return "Three";
                case 4:
                    return "Four";
                case 5:
                    return "Five";
                case 6:
                    return "Six";
                case 7:
                    return "Seven";
                case 8:
                    return "Eight";
                case 9:
                    return "Nine";
                default:
                    return "ERROR";
            }
        }
    }
</string></string></string></string></string></string>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ron Beyer
President 6D Systems LLC
United States United States
I studied Software Engineering at Milwaukee School of Engineering for 2 years before switching to Management of Information Systems for a more business oriented approach. I've been developing software since the age of 14, and have waded through languages such as QBasic, TrueBasic, C, C++, Java, VB6, VB.NET, C#, etc. I've been developing professionally since 2002 in .NET.
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 3 rtyhrhrjhtmemberAlireza_HisysteM19-Apr-11 5:03 
Reason for my vote of 3
rtyhrhrjht
GeneralI wasn't real careful with things like spelling and commenti...memberSilic0re0924-Aug-10 4:53 
I wasn't real careful with things like spelling and commenting just because it's an alternative to a tip/trick. If I posted it as an article or my own tip/trick I probably would have included a sample project fully commented and spell checked. I whipped this up in about 10 minutes.
GeneralI saw some COBOL code for this nearly 30 years ago and it wa...memberYeoville23-Aug-10 12:36 
I saw some COBOL code for this nearly 30 years ago and it was full of errors, simply because English is not a very regular language. Here "forty" and "ninety" have been misspelt (typos?). And usually we say "twelve thousand, three hundred..." as opposed to "twelve thousand and three hundred...". And not even stepping into the discussion of English and US versions of "billion"!
GeneralReason for my vote of 5 Simplicity, robustness. Could improv...memberKamran Behzad23-Aug-10 12:32 
Reason for my vote of 5
Simplicity, robustness. Could improve with better commenting.
GeneralCan I please start the 1000,000,000 is a thousand million no...memberMember 468474523-Aug-10 10:54 
Can I please start the 1000,000,000 is a thousand million not a billion argument.. Or should it be the 'is 1000,000,000,000 a thousand billion or a million million or a trillion? - no its a billion' argument.
GeneralReason for my vote of 2 poormemberToli Cuturicu22-Aug-10 7:42 
Reason for my vote of 2
poor
GeneralReason for my vote of 5 bestmemberdivyang448221-Aug-10 9:28 
Reason for my vote of 5
best
GeneralIf you are going to post code, at least check it works! Try ...mvpOriginalGriff20-Aug-10 23:20 
If you are going to post code, at least check it works! Try testing it with 1000000: "One Million Thousand" is not correct!
Generallol.. you people fail to see Silic0re09 sarcasm! You're even...memberSimon Dufour20-Aug-10 8:23 
lol.. you people fail to see Silic0re09 sarcasm! You're even worse!
GeneralReason for my vote of 1 Failed to detect sarcarm.memberleppie19-Aug-10 22:05 
Reason for my vote of 1
Failed to detect sarcarm.
GeneralYou missed the joke my friend :)memberMustafa Ismail Mustafa19-Aug-10 9:26 
You missed the joke my friend Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 19 Aug 2010
Article Copyright 2010 by Ron Beyer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid