tags:

views:

296

answers:

8

Possible Duplicate:
C# Convert Integers into Written Numbers

I need to take an integer value and convert it to its english word equivalent (i.e. 4 => "four", 1879 => "one thousand, eight hundred seventy-nine") in .NET (3.5 to be specific).

I'm wondering if there is anything built into the .NET framework for making such a conversion. Seems like it would be useful enough to belong there. I haven't been able to find anything to do the job.

If it isn't included in the framework anywhere, does anyone have any ideas more elegant than a digit/place specific lookup?

+2  A: 

This looks to do the trick, so long as you aren't going to be dealing with anything past the trillions.

http://weblogs.asp.net/Justin_Rogers/archive/2004/06/09/151675.aspx

AlexCuse
A: 

There isn't anything built in that I'm aware of. You're just going to have to do some digit-parsing and replacing.

I did find some examples on the net:

http://www.dotnetspider.com/resources/2743-Code-Convert-numbers-word.aspx http://www.codeproject.com/KB/cs/codesamples.aspx

Randolpho
+2  A: 

Check out this link: Functional Fun: Euler 17 for a LINQ solution.

Tony k
+5  A: 

http://stackoverflow.com/questions/309884/code-golf-number-to-words

Check this question.

mandaleeka
So there is no built-in function. Right?
Mahatma
nope there is not.
Anirudh Goel
A: 

There is no direct function to convert and create word forms for you. You will have to write your program, where essentially you will have to hardcode values of all digits i.e. 1 one,2 two,...,9 nine. Then also you will have to take care of tens, hundreds, thousands, and then you will have to write logic to extract digits and append the words accordingly.

Anirudh Goel
A: 

Nothing inbuilt. Here is a way to do this.

danish
+3  A: 

string s being the input number

        string s;
        int input;
        string[] placement = { "thousand", "hundred", "ten", "" };
        string[] numbersToLetters = { "", "one", "two", "tre", "four", "five", "six", "seven", "eight", "nine" };

        for (int i = 0; i < s.Length; i++)
        {

            int digits = 3 - i; //placement[3] is the maximum value in the array. Since you can't have an input with the value NULL.
            //-i negates the loop value i. E.g this will produce an output of nothing.

            if (s[i] - 48 != 0){ //Make sure input is not zero. There is no such thing as zeroten.
                digits = 4 - s.Length; 
            }
            int result = int.Parse(s[i].ToString()); //Convert char unicode to regular in

            Console.Write(numbersToLetters[result + placering[i + digits]);
        }
CasperT