Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Converting numbers to the word equivalent.

4.91/5 (77 votes)
12 May 2013CPOL1 min read 153K  
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)