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

Converting numbers to the word equivalent.

By , 12 May 2013
 
If your tutor asks you to convert numbers to words (i.e. 565 to a string "Five hundred and sixty-five") then you are not alone! This question gets asked here very frequently, so here is the simple, foolproof way to solve your problem. It shows the outline of how to do it, and it is then up to you to fill in the detail, comments etc. - otherwise every-bodies would look the same, and you might get caught for copying!
        public static string ConvertToWords(int number)
            {
            switch (number)
                {
                case 0:
                    return "Zero";
                case 1:
                    return "One";
                ...
                case 568:
                    return "Five hundred and sixty-eight";
                case 569:
                    return "Five hundred and sixty-nine";
                }
            throw new ArgumentOutOfRangeException("Number greater than infinity!");
            }
 
I cannot remember who originally posted this coding gem, and I can't find it with a quick search, but if you know who did post it let me know and I will provide full credit.
 

The original version of this software was written by Smithers-Jones[^] in C++ and is available here[^] My thanks for the inspiration and the excellent work!
 
I am indebted to Luc Pattyn for pointing out the error in versions before V3.0 regarding the placement of a hyphen between the tens and unit digits. He correctly pointed out here[^] that numbers should not be written as "twenty one" but as "twenty-one".
 
[edit]V2.0: Credit for original version added - OriginalGriff[/edit]
[edit]V3.0: Corrected the hyphenation between the tens digit and the units digit - OriginalGriff[/edit]
[edit]V4.0: Link to the message of Luc Pattyn corrected - ProgramFOX[/edit]

License

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

About the Author

OriginalGriff
CEO
Wales Wales
Member
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberTnTinMn4hrs 38mins ago 
QuestionIt reminds me...memberBarrRobot7hrs 15mins ago 
GeneralMy vote of 5!professionalMatt T Heffron9hrs 31mins ago 
QuestionYou gotta be kidding... You voted this 5 stars?membermarsman_CA12hrs 34mins ago 
AnswerRe: You gotta be kidding... You voted this 5 stars?membermarsman_CA12hrs 31mins ago 
GeneralRe: You gotta be kidding... You voted this 5 stars?professionalRavi Bhavnani12hrs 10mins ago 
GeneralRe: You gotta be kidding... You voted this 5 stars?protectorOriginalGriff11hrs 45mins ago 
GeneralRe: You gotta be kidding... You voted this 5 stars?protectorOriginalGriff11hrs 44mins ago 
QuestionAn optimised answermemberJuan Falgueras Cano12hrs 56mins ago 
GeneralMy vote of 4memberJuan Falgueras Cano12hrs 58mins ago 
SuggestionNice, but...professionalRavi Bhavnani16hrs 1 min ago 
QuestionFunny!memberDidiKunz21hrs 57mins ago 
Questionmy vote of 5professionalSimon_Whale22hrs 45mins ago 
AnswerRe: my vote of 5protectorOriginalGriff22hrs 33mins ago 
GeneralMy vote of 4professionalMarco Bertschi12 May '13 - 9:37 
GeneralRe: My vote of 4protectorOriginalGriff23hrs 49mins ago 
GeneralMy vote of 1memberStephen Hardie12 May '13 - 8:19 
GeneralRe: My vote of 1professionalMarco Bertschi12 May '13 - 9:34 
GeneralMy vote of 5 + There is a shorter waymember ProgramFOX12 May '13 - 6:57 
QuestionMy vote of 5memberJackDingler14 Jan '13 - 7:15 
AnswerRe: My vote of 5professionalMarco Bertschi12 May '13 - 9:35 
GeneralMy vote of 5memberSoMad10 Jul '12 - 17:01 
GeneralRe: My vote of 5mvpOriginalGriff10 Jul '12 - 21:50 
GeneralRe: My vote of 5memberSoMad10 Jul '12 - 22:02 
GeneralGood attempt but you'll have to write it for every number. t...memberJavedsamrt29 Nov '11 - 21:35 
GeneralRe: Good attempt but you'll have to write it for every number. t...memberDidiKunz22hrs ago 
GeneralReason for my vote of 5 Yep, that solution efficiently gets ...memberDoc Lobster6 Nov '11 - 22:00 
Generalis this some kind of joke???memberdanny3339 Oct '11 - 4:47 
GeneralReason for my vote of 1 very bad solution u need to write ea...memberbashar Haydar6 Oct '10 - 20:35 
GeneralWhoosh! That or perhaps some people want it to work for numb...memberEdbert P19 Aug '10 - 20:13 
GeneralReason for my vote of 1 Voted 1 because it's not a good tip....memberSimon Dufour19 Aug '10 - 11:38 
General"hey griff, you are maybe thinking about this" That's the on...mvpOriginalGriff19 Aug '10 - 6:32 
Generalhey griff, you are maybe thinking about this (http://www.cod...memberSmithers-Jones19 Aug '10 - 5:32 
GeneralReason for my vote of 5 By the way, what if I need to handle...memberemartinho19 Aug '10 - 5:18 
GeneralReason for my vote of 5 Finally! But can you help me with th...memberYayozama19 Aug '10 - 4:11 
GeneralReally awesomememberHimanshuJoshi19 Aug '10 - 3:46 
GeneralReason for my vote of 5 sir, i copied your code now my teach...memberYusuf19 Aug '10 - 3:27 
GeneralReason for my vote of 5 Lovely! And the first post which has...memberAnkur\m/19 Aug '10 - 3:24 
GeneralDoesn't everyone do it like this? :)memberMike Hankey19 Aug '10 - 3:19 
GeneralYou misspelled sixty-eight and sixty-nine and forgot to use ...memberEnnis Ray Lynch, Jr.19 Aug '10 - 3:16 
GeneralYou can improve it a bit though - forgot about negative numb...memberSvetlin Panayotov19 Aug '10 - 3:15 
GeneralReason for my vote of 5 So brilliant and simple! There's eve...memberSvetlin Panayotov19 Aug '10 - 3:13 
GeneralI'd like to see some unit tests, your method seems a bit err...mvpLuc Pattyn19 Aug '10 - 2:54 
GeneralReason for my vote of 5 Good one! :DmemberNuri Ismail19 Aug '10 - 2:47 
GeneralReason for my vote of 5 Lost for words. Can you translate th...memberdigital man19 Aug '10 - 2:42 
GeneralReason for my vote of 5 Excellent!memberhammerstein0519 Aug '10 - 2:35 
GeneralReason for my vote of 5 Fantastic! :)memberAlison P19 Aug '10 - 2:20 
GeneralReason for my vote of 5 Funnymemberlinuxjr19 Aug '10 - 2:18 
GeneralReason for my vote of 5 Good one!!!memberJohnny J.19 Aug '10 - 2:09 
GeneralReason for my vote of 5 Does it work just for the English la...mvpCPallini19 Aug '10 - 2:04 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 12 May 2013
Article Copyright 2010 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid