Click here to Skip to main content
11,428,324 members (63,460 online)
Click here to Skip to main content

Converting numbers to the word equivalent.

, 12 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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?
Follow on   Google+

Comments and Discussions

 
SuggestionBe serious :-) Pin
rwtrecasd14-May-13 7:11
memberrwtrecasd14-May-13 7:11 
GeneralMy vote of 5 Pin
TnTinMn13-May-13 17:42
memberTnTinMn13-May-13 17:42 
QuestionIt reminds me... Pin
BarrRobot13-May-13 15:05
memberBarrRobot13-May-13 15:05 
GeneralMy vote of 5! Pin
Matt T Heffron13-May-13 12:49
professionalMatt T Heffron13-May-13 12:49 
QuestionYou gotta be kidding... You voted this 5 stars? Pin
marsman_CA13-May-13 9:46
membermarsman_CA13-May-13 9:46 
AnswerRe: You gotta be kidding... You voted this 5 stars? Pin
marsman_CA13-May-13 9:49
membermarsman_CA13-May-13 9:49 
GeneralRe: You gotta be kidding... You voted this 5 stars? Pin
Ravi Bhavnani13-May-13 10:10
professionalRavi Bhavnani13-May-13 10:10 
GeneralRe: You gotta be kidding... You voted this 5 stars? Pin
OriginalGriff13-May-13 10:35
protectorOriginalGriff13-May-13 10:35 
GeneralRe: You gotta be kidding... You voted this 5 stars? Pin
OriginalGriff13-May-13 10:36
protectorOriginalGriff13-May-13 10:36 
QuestionAn optimised answer Pin
Juan Falgueras Cano13-May-13 9:24
memberJuan Falgueras Cano13-May-13 9:24 
GeneralMy vote of 4 Pin
Juan Falgueras Cano13-May-13 9:22
memberJuan Falgueras Cano13-May-13 9:22 
SuggestionNice, but... Pin
Ravi Bhavnani13-May-13 6:19
professionalRavi Bhavnani13-May-13 6:19 
QuestionFunny! Pin
DidiKunz13-May-13 0:23
memberDidiKunz13-May-13 0:23 
Questionmy vote of 5 Pin
Simon_Whale12-May-13 23:35
professionalSimon_Whale12-May-13 23:35 
AnswerRe: my vote of 5 Pin
OriginalGriff12-May-13 23:47
protectorOriginalGriff12-May-13 23:47 
GeneralMy vote of 4 Pin
Marco Bertschi12-May-13 10:37
professionalMarco Bertschi12-May-13 10:37 
GeneralRe: My vote of 4 Pin
OriginalGriff12-May-13 22:31
protectorOriginalGriff12-May-13 22:31 
GeneralMy vote of 1 Pin
Stephen Hardie12-May-13 9:19
memberStephen Hardie12-May-13 9:19 
GeneralRe: My vote of 1 Pin
Marco Bertschi12-May-13 10:34
professionalMarco Bertschi12-May-13 10:34 
GeneralMy vote of 5 + There is a shorter way Pin
ProgramFOX12-May-13 7:57
member ProgramFOX12-May-13 7:57 
QuestionMy vote of 5 Pin
JackDingler14-Jan-13 8:15
memberJackDingler14-Jan-13 8:15 
AnswerRe: My vote of 5 Pin
Marco Bertschi12-May-13 10:35
professionalMarco Bertschi12-May-13 10:35 
GeneralMy vote of 5 Pin
SoMad10-Jul-12 18:01
memberSoMad10-Jul-12 18:01 
GeneralRe: My vote of 5 Pin
OriginalGriff10-Jul-12 22:50
mvpOriginalGriff10-Jul-12 22:50 
GeneralRe: My vote of 5 Pin
SoMad10-Jul-12 23:02
memberSoMad10-Jul-12 23:02 
GeneralGood attempt but you'll have to write it for every number. t... Pin
Javedsamrt29-Nov-11 22:35
memberJavedsamrt29-Nov-11 22:35 
GeneralRe: Good attempt but you'll have to write it for every number. t... Pin
DidiKunz13-May-13 0:20
memberDidiKunz13-May-13 0:20 
GeneralReason for my vote of 5 Yep, that solution efficiently gets ... Pin
Doc Lobster6-Nov-11 23:00
memberDoc Lobster6-Nov-11 23:00 
Generalis this some kind of joke??? Pin
danny3339-Oct-11 5:47
memberdanny3339-Oct-11 5:47 
GeneralReason for my vote of 1 very bad solution u need to write ea... Pin
bashar Haydar6-Oct-10 21:35
memberbashar Haydar6-Oct-10 21:35 
GeneralWhoosh! That or perhaps some people want it to work for numb... Pin
Edbert P19-Aug-10 21:13
memberEdbert P19-Aug-10 21:13 
GeneralReason for my vote of 1 Voted 1 because it's not a good tip.... Pin
Simon Dufour19-Aug-10 12:38
memberSimon Dufour19-Aug-10 12:38 
General"hey griff, you are maybe thinking about this" That's the on... Pin
OriginalGriff19-Aug-10 7:32
mvpOriginalGriff19-Aug-10 7:32 
Generalhey griff, you are maybe thinking about this (http://www.cod... Pin
Smithers-Jones19-Aug-10 6:32
memberSmithers-Jones19-Aug-10 6:32 
hey griff, you are maybe thinking about this (http://www.codeproject.com/Answers/79014/Conver-number-into-word.aspx#answer2) from Q&A? Smile | :)
GeneralReason for my vote of 5 By the way, what if I need to handle... Pin
emartinho19-Aug-10 6:18
memberemartinho19-Aug-10 6:18 
GeneralReason for my vote of 5 Finally! But can you help me with th... Pin
Yayozama19-Aug-10 5:11
memberYayozama19-Aug-10 5:11 
GeneralReally awesome Pin
HimanshuJoshi19-Aug-10 4:46
memberHimanshuJoshi19-Aug-10 4:46 
GeneralReason for my vote of 5 sir, i copied your code now my teach... Pin
Yusuf19-Aug-10 4:27
memberYusuf19-Aug-10 4:27 
GeneralReason for my vote of 5 Lovely! And the first post which has... Pin
Ankur\m/19-Aug-10 4:24
memberAnkur\m/19-Aug-10 4:24 
GeneralDoesn't everyone do it like this? :) Pin
Mike Hankey19-Aug-10 4:19
memberMike Hankey19-Aug-10 4:19 
GeneralYou misspelled sixty-eight and sixty-nine and forgot to use ... Pin
Ennis Ray Lynch, Jr.19-Aug-10 4:16
memberEnnis Ray Lynch, Jr.19-Aug-10 4:16 
GeneralYou can improve it a bit though - forgot about negative numb... Pin
Svetlin Panayotov19-Aug-10 4:15
memberSvetlin Panayotov19-Aug-10 4:15 
GeneralReason for my vote of 5 So brilliant and simple! There's eve... Pin
Svetlin Panayotov19-Aug-10 4:13
memberSvetlin Panayotov19-Aug-10 4:13 
GeneralI'd like to see some unit tests, your method seems a bit err... Pin
Luc Pattyn19-Aug-10 3:54
mvpLuc Pattyn19-Aug-10 3:54 
GeneralReason for my vote of 5 Good one! :D Pin
Nuri Ismail19-Aug-10 3:47
memberNuri Ismail19-Aug-10 3:47 
GeneralReason for my vote of 5 Lost for words. Can you translate th... Pin
digital man19-Aug-10 3:42
memberdigital man19-Aug-10 3:42 
GeneralReason for my vote of 5 Excellent! Pin
hammerstein0519-Aug-10 3:35
memberhammerstein0519-Aug-10 3:35 
GeneralReason for my vote of 5 Fantastic! :) Pin
Alison P19-Aug-10 3:20
memberAlison P19-Aug-10 3:20 
GeneralReason for my vote of 5 Funny Pin
linuxjr19-Aug-10 3:18
memberlinuxjr19-Aug-10 3:18 
GeneralReason for my vote of 5 Good one!!! Pin
Johnny J.19-Aug-10 3:09
memberJohnny J.19-Aug-10 3:09 

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

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

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