70

Possible Duplicate:
How can I convert an integer into its verbal representation?

Can anybody give me a primer code I could work on in converting numbers into words?

Converting numbers to words (ranging from -1000 to +1000) example: 1000 --> one thousand

Community
  • 1
  • 1
Juan De La Cruz
  • 837
  • 1
  • 7
  • 5
  • As in converting numbers into strings? Or converting numbers into word representations of that number, e.g. 2030 = two thousand thirty? – froadie Apr 28 '10 at 13:24
  • 6
    this looks exactly like Project Euler question 17: http://projecteuler.net/index.php?section=problems&id=17 – Ben McCormack Apr 28 '10 at 13:33
  • I have actually written such a piece of code but it works for Czech language only. You're in an easier position as there are some pecularities to Czech grammar such as cases etc. which you don't have to deal with in English. – trendl Apr 28 '10 at 13:34
  • 3
    Here's how I did it: http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words – Jonathan Wood Dec 29 '10 at 06:25

2 Answers2

228
public static string NumberToWords(int number)
{
    if (number == 0)
        return "zero";

    if (number < 0)
        return "minus " + NumberToWords(Math.Abs(number));

    string words = "";

    if ((number / 1000000) > 0)
    {
        words += NumberToWords(number / 1000000) + " million ";
        number %= 1000000;
    }

    if ((number / 1000) > 0)
    {
        words += NumberToWords(number / 1000) + " thousand ";
        number %= 1000;
    }

    if ((number / 100) > 0)
    {
        words += NumberToWords(number / 100) + " hundred ";
        number %= 100;
    }

    if (number > 0)
    {
        if (words != "")
            words += "and ";

        var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

        if (number < 20)
            words += unitsMap[number];
        else
        {
            words += tensMap[number / 10];
            if ((number % 10) > 0)
                words += "-" + unitsMap[number % 10];
        }
    }

    return words;
}
nawfal
  • 70,104
  • 56
  • 326
  • 368
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • made it a neat extension method, if you dont mind.. – nawfal Nov 12 '12 at 05:44
  • @nawfal: the recursion to NumberToWords is not correct after your edit. – Tom Pažourek Dec 16 '12 at 12:24
  • 3
    @tomp I rolled back to previous version, not because I couldnt have corrected the typo, but that an extension method on an int for this doesnt look nice.. – nawfal Dec 16 '12 at 18:27
  • 6
    Though old, someone has referenced this answer...the arrays should be defined as `static`, otherwise, they will be instantiated on every call which could be multiple times for a single conversion due to the recursion. – Kevin Brock Apr 01 '13 at 17:03
  • 2
    Very minor problem when dealing with numbers such as 100013, the result is "one hundred thousand and thirteen" (note that there is two spaces between the word "hundred" and "thousand"). Could fix this by calling a static method that only appends a space if there is not already a space at the end of the string. – Contango Jun 28 '13 at 13:10
  • The word "and" shouldn't be used here since "and" commonly represents a decimal point. – bugfixr Jul 12 '14 at 17:22
  • 2
    Not sure why you thought the extension method for an int doesn't look nice? I used your method as an extension to int, but renamed it "ToWords", which seems pretty intuitive. – Fijjit Aug 03 '15 at 18:21
  • 1
    Can we implement this using DI pattern or any other OOP concepts? – Pranav Bilurkar Jun 06 '20 at 08:30
2

When I had to solve this problem, I created a hard-coded data dictionary to map between numbers and their associated words. For example, the following might represent a few entries in the dictionary:

{1, "one"}
{2, "two"}
{30, "thirty"}

You really only need to worry about mapping numbers in the 10^0 (1,2,3, etc.) and 10^1 (10,20,30) positions because once you get to 100, you simply have to know when to use words like hundred, thousand, million, etc. in combination with your map. For example, when you have a number like 3,240,123, you get: three million two hundred forty thousand one hundred twenty three.

After you build your map, you need to work through each digit in your number and figure out the appropriate nomenclature to go with it.

Ben McCormack
  • 32,086
  • 48
  • 148
  • 223