Click here to Skip to main content
15,867,834 members
Articles / Programming Languages / C#
Tip/Trick

Converting numbers to the word equivalent.

Rate me:
Please Sign up or sign in to vote.
4.91/5 (77 votes)
12 May 2013CPOL1 min read 130.8K   14   59
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)


Written By
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

 
SuggestionBe serious :-) Pin
Alberto Armando14-May-13 6:11
Alberto Armando14-May-13 6:11 
GeneralMy vote of 5 Pin
TnTinMn13-May-13 16:42
TnTinMn13-May-13 16:42 
QuestionIt reminds me... Pin
BarrRobot13-May-13 14:05
BarrRobot13-May-13 14:05 
GeneralMy vote of 5! Pin
Matt T Heffron13-May-13 11:49
professionalMatt T Heffron13-May-13 11:49 
QuestionYou gotta be kidding... You voted this 5 stars? Pin
marsman_CA13-May-13 8:46
professionalmarsman_CA13-May-13 8:46 
AnswerRe: You gotta be kidding... You voted this 5 stars? Pin
marsman_CA13-May-13 8:49
professionalmarsman_CA13-May-13 8:49 
GeneralRe: You gotta be kidding... You voted this 5 stars? Pin
Ravi Bhavnani13-May-13 9:10
professionalRavi Bhavnani13-May-13 9:10 
GeneralRe: You gotta be kidding... You voted this 5 stars? Pin
OriginalGriff13-May-13 9:35
mveOriginalGriff13-May-13 9:35 
GeneralRe: You gotta be kidding... You voted this 5 stars? Pin
OriginalGriff13-May-13 9:36
mveOriginalGriff13-May-13 9:36 
QuestionAn optimised answer Pin
Juan Falgueras Cano13-May-13 8:24
Juan Falgueras Cano13-May-13 8:24 
GeneralMy vote of 4 Pin
Juan Falgueras Cano13-May-13 8:22
Juan Falgueras Cano13-May-13 8:22 
SuggestionNice, but... Pin
Ravi Bhavnani13-May-13 5:19
professionalRavi Bhavnani13-May-13 5:19 
QuestionFunny! Pin
DidiKunz12-May-13 23:23
DidiKunz12-May-13 23:23 
Questionmy vote of 5 Pin
Simon_Whale12-May-13 22:35
Simon_Whale12-May-13 22:35 
AnswerRe: my vote of 5 Pin
OriginalGriff12-May-13 22:47
mveOriginalGriff12-May-13 22:47 
GeneralMy vote of 4 Pin
Marco Bertschi12-May-13 9:37
protectorMarco Bertschi12-May-13 9:37 
GeneralRe: My vote of 4 Pin
OriginalGriff12-May-13 21:31
mveOriginalGriff12-May-13 21:31 
GeneralMy vote of 1 Pin
Stephen Brannan12-May-13 8:19
Stephen Brannan12-May-13 8:19 
GeneralRe: My vote of 1 Pin
Marco Bertschi12-May-13 9:34
protectorMarco Bertschi12-May-13 9:34 
GeneralMy vote of 5 + There is a shorter way Pin
Thomas Daniels12-May-13 6:57
mentorThomas Daniels12-May-13 6:57 
QuestionMy vote of 5 Pin
JackDingler14-Jan-13 7:15
JackDingler14-Jan-13 7:15 
AnswerRe: My vote of 5 Pin
Marco Bertschi12-May-13 9:35
protectorMarco Bertschi12-May-13 9:35 
GeneralMy vote of 5 Pin
SoMad10-Jul-12 17:01
professionalSoMad10-Jul-12 17:01 
GeneralRe: My vote of 5 Pin
OriginalGriff10-Jul-12 21:50
mveOriginalGriff10-Jul-12 21:50 
GeneralRe: My vote of 5 Pin
SoMad10-Jul-12 22:02
professionalSoMad10-Jul-12 22:02 

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.