Click here to Skip to main content
14,209,465 members
Click here to Skip to main content
Tip/Trick
Posted 19 Aug 2010

Tagged as

Stats

81.8K views
14 bookmarked

Converting numbers to the word equivalent.

,
Rate this:
4.91 (77 votes)
Please Sign up or sign in to vote.
4.91 (77 votes)
12 May 2013     CPOL    
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! Here is the simplest, most foolproof way to do it.
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)

Share

About the Author

OriginalGriff
CEO
Wales Wales
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?

Comments and Discussions

 
GeneralYou misspelled sixty-eight and sixty-nine and forgot to use ... Pin
Ennis Ray Lynch, Jr.19-Aug-10 3:16
memberEnnis Ray Lynch, Jr.19-Aug-10 3:16 
GeneralYou can improve it a bit though - forgot about negative numb... Pin
Svetlin Panayotov19-Aug-10 3:15
memberSvetlin Panayotov19-Aug-10 3:15 
GeneralReason for my vote of 5 So brilliant and simple! There's eve... Pin
Svetlin Panayotov19-Aug-10 3:13
memberSvetlin Panayotov19-Aug-10 3:13 
GeneralI'd like to see some unit tests, your method seems a bit err... Pin
Luc Pattyn19-Aug-10 2:54
professionalLuc Pattyn19-Aug-10 2:54 
GeneralReason for my vote of 5 Good one! :D Pin
Nuri Ismail19-Aug-10 2:47
memberNuri Ismail19-Aug-10 2:47 
GeneralReason for my vote of 5 Lost for words. Can you translate th... Pin
R. Giskard Reventlov19-Aug-10 2:42
professionalR. Giskard Reventlov19-Aug-10 2:42 
GeneralReason for my vote of 5 Excellent! Pin
hammerstein0519-Aug-10 2:35
memberhammerstein0519-Aug-10 2:35 
GeneralReason for my vote of 5 Fantastic! :) Pin
LittleYellowBird19-Aug-10 2:20
memberLittleYellowBird19-Aug-10 2:20 
Reason for my vote of 5
Fantastic! Smile | :)
GeneralReason for my vote of 5 Funny Pin
linuxjr19-Aug-10 2:18
professionallinuxjr19-Aug-10 2:18 
GeneralReason for my vote of 5 Good one!!! Pin
Johnny J.19-Aug-10 2:09
professionalJohnny J.19-Aug-10 2:09 
GeneralReason for my vote of 5 Does it work just for the English la... Pin
CPallini19-Aug-10 2:04
mveCPallini19-Aug-10 2:04 
GeneralReason for my vote of 5 funny Pin
Peter Mulholland19-Aug-10 2:01
memberPeter Mulholland19-Aug-10 2:01 
General"no good - swjam" Swjam - are you saying it does not work? A... Pin
OriginalGriff19-Aug-10 1:58
protectorOriginalGriff19-Aug-10 1:58 
GeneralRe: "no good - swjam"Swjam - are you saying it does not work? A... Pin
swjam8-Aug-12 3:13
memberswjam8-Aug-12 3:13 
GeneralReason for my vote of 1 no good Pin
swjam19-Aug-10 1:55
memberswjam19-Aug-10 1:55 
GeneralReason for my vote of 5 Because it's genius! Pin
Mark_Wallace19-Aug-10 1:48
memberMark_Wallace19-Aug-10 1:48 
GeneralThis looks good, but how do you handle negative numbers? :) Pin
Chris Meech19-Aug-10 1:46
memberChris Meech19-Aug-10 1:46 
General5 Points for being Original, Funny, Sarcastic and Ironic in ... Pin
Dalek Dave19-Aug-10 1:42
professionalDalek Dave19-Aug-10 1:42 
GeneralAWESOME! MY HERO! Pin
leppie19-Aug-10 1:39
memberleppie19-Aug-10 1:39 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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