converting numbers in to words C# [duplicate]
Asked Answered
c#
E

2

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

Enscroll answered 28/4, 2010 at 13:22 Comment(4)
As in converting numbers into strings? Or converting numbers into word representations of that number, e.g. 2030 = two thousand thirty?Valero
this looks exactly like Project Euler question 17: projecteuler.net/index.php?section=problems&id=17Devon
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.Exponential
Here's how I did it: blackbeltcoder.com/Articles/strings/converting-numbers-to-wordsHarms
G
229
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;
}
Ge answered 28/4, 2010 at 14:34 Comment(9)
made it a neat extension method, if you dont mind..Tisbe
@nawfal: the recursion to NumberToWords is not correct after your edit.Increment
@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..Tisbe
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.Escaut
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.Whencesoever
The word "and" shouldn't be used here since "and" commonly represents a decimal point.Boot
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.Ungley
Can we implement this using DI pattern or any other OOP concepts?Teamwork
"And" isn't correct here. For example, take the following two different numbers: 200 and 4... or two hundred and four, vs 204 and 4, or two hundred four and four.Devito
D
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.

Devon answered 28/4, 2010 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.