Convert numbers to C # words

Possible duplicate:
How to convert an integer to its verbal representation?

Can someone give me a primer code that I could work on when converting numbers to words?

Converting numbers to words (from -1000 to +1000) example: 1000 → thousand

+54
c #
Apr 28 '10 at 13:22
source share
3 answers
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; } 
+173
Apr 28 2018-10-14T00:
source share

When I had to solve this problem, I created a dictionary with hard-coded data to match numbers and related words. For example, the following number may represent several entries in a dictionary:

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

You really only need to worry about matching numbers at positions 10 ^ 0 (1,2,3, etc.) and 10 ^ 1 (10,20,30), because as soon as you get to 100, you just need to know when to use words like hundreds, thousand, a million, etc. combined with your card. For example, if you have a number, for example, 3240123, you get: three million two hundred forty thousand one hundred twenty three .

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

+1
Apr 28 '10 at
source share

You really need to provide more details about what you mean. Do you mean "words" or "lines"?

For example, if you want to convert a number to a string, you only need something like this:

 int i = 123; string text = i.ToString(); 

You can actually do this:

 (123).ToString(); // u need to put number in parenthesis 

and even

 (123.5).ToString(); // this always trips me out 

However, if you need to convert 123 to one hundred twenty three , you need to do more parsing. You will have to break the number on its part, for example, hundreds, tenths, etc.

You can start by getting the length of the string (for ints) to figure out where to start the breakdown. For example, 123 has 3 digits, so let N = 3 and I = 1. Then you start by dividing 123 by 10 (Ni) or 100. This gives you 1. Now you know that the word starts with "one hundred." Then add i, subtract this number (100) and divide by 10 (Ni) or 10 - this will give you 2. Do this until N == i.

Hope this helps. You must really change your question.

-5
Apr 28 '10 at
source share



All Articles