Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting numbers in to words C# [duplicate]

Tags:

c#

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

like image 739
Juan De La Cruz Avatar asked Apr 28 '10 13:04

Juan De La Cruz


People also ask

How do you convert numbers into words?

Type the formula =SpellNumber(A1) into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber(22.50).

How do you turn a number into a character?

char a = Character. forDigit(num1, 10); We have used the forDigit() method converts the specified int value into char value. Here, 10 and 16 are radix values for decimal and hexadecimal numbers respectively.


2 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; } 
like image 100
LukeH Avatar answered Oct 14 '22 19:10

LukeH


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.

like image 45
Ben McCormack Avatar answered Oct 14 '22 18:10

Ben McCormack