Created
July 19, 2015 17:44
-
-
Save VegaFromLyra/0b7d25b086fb4c115476 to your computer and use it in GitHub Desktop.
Numbers to words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
// Convert a number to its english form | |
// 123 -> One hundred twenty three | |
// 123456 - > One hundred twenty three thousand four hundred fifty six | |
// 1234567 - > One million two hundred thirty four thousand five hundred sixty seven | |
// 12345678 -> twelve million three hundred forty five thousand six hundred seventy eight | |
// 123456789 -> one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine | |
// 1234567890 -> one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety | |
namespace numbersToWords | |
{ | |
public class Program | |
{ | |
static Dictionary<int, string> map; | |
public static void Main(string[] args) | |
{ | |
map = new Dictionary<int, string>(); | |
map.Add(0, ""); | |
map.Add(1, "one"); | |
map.Add(2, "two"); | |
map.Add(3, "three"); | |
map.Add(4, "four"); | |
map.Add(5, "five"); | |
map.Add(6, "six"); | |
map.Add(7, "seven"); | |
map.Add(8, "eight"); | |
map.Add(9, "nine"); | |
map.Add(10, "ten"); | |
map.Add(11, "eleven"); | |
map.Add(12, "twelve"); | |
map.Add(13, "thirteen"); | |
map.Add(14, "fourteen"); | |
map.Add(15, "fifteen"); | |
map.Add(16, "sixteen"); | |
map.Add(17, "seventeen"); | |
map.Add(18, "eighteen"); | |
map.Add(19, "nineteen"); | |
map.Add(20, "twenty"); | |
map.Add(30, "thirty"); | |
map.Add(40, "forty"); | |
map.Add(50, "fifty"); | |
map.Add(60, "sixty"); | |
map.Add(70, "seventy"); | |
map.Add(80, "eighty"); | |
map.Add(90, "ninety"); | |
map.Add(100, "hundred"); | |
map.Add(1000, "thousand"); | |
map.Add(1000000, "million"); | |
map.Add(1000000000, "billion"); | |
test(123); | |
test(1234); | |
test(12345); | |
test(123456); | |
test(1234567); | |
test(12345678); | |
test(123456789); | |
test(1234567890); | |
} | |
static void test(int num) { | |
Console.WriteLine("Converted form of {0} is {1}", num, convertToWords(num)); | |
} | |
static string convertToWords(int number) { | |
StringBuilder result = new StringBuilder(""); | |
int position = 1; | |
while (number > 0) { | |
int part = number % 1000; | |
result.Insert(0, convertThreeDigit(part) + " "); | |
number = number / 1000; | |
position *= (int)Math.Pow(10, 3); | |
if (number > 0) { | |
result.Insert(0, map[position] + " "); | |
} | |
} | |
return result.ToString(); | |
} | |
static string convertThreeDigit(int num) { | |
if (map.ContainsKey(num)) { | |
return map[num]; | |
} | |
StringBuilder result = new StringBuilder(""); | |
int position = 0; | |
if ((num % 100) >= 11 && (num % 100) <= 19) { | |
int part = num % 100; | |
result.Insert(0, map[part] + " "); | |
num = num / 100; | |
position += 2; | |
} | |
while (num > 0) { | |
if (position == 2) { | |
result.Insert(0, "hundred "); | |
result.Insert(0, map[num] + " "); | |
} else { | |
int part = (num % 10) * (int)Math.Pow(10, position); | |
var str = map[part]; | |
if (!String.IsNullOrEmpty(str)) { | |
result.Insert(0, str + " "); | |
} | |
} | |
num = num / 10; | |
position++; | |
} | |
return result.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment