Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- class NumbersAsWords
- {
- static void Main()
- {
- for (int i = 0; i < 1000; i++)
- {
- Console.WriteLine("{0,3}: {1}", i, GetNumberAsWords(i));
- }
- }
- static string GetNumberAsWords(int a)
- {
- string sentence = "";
- string word = "";
- if (a >= 100 && a < 1000)
- {
- word = GetDigitAsWord(a / 100);
- if (sentence == "")
- {
- sentence += FirstLetterToUpper(word);
- }
- else
- {
- sentence += word;
- }
- sentence += " hundred";
- }
- if ((a >= 10 && a < 20) || (a % 100 >= 10 && a % 100 < 20))
- {
- if (a >= 100 && a < 1000 && a % 100 != 0)
- {
- sentence += " and ";
- }
- word = Get10To19AsWord(a % 100);
- if (sentence == "")
- {
- sentence += FirstLetterToUpper(word);
- }
- else
- {
- sentence += word;
- }
- return sentence;
- }
- if ((a >= 20 && a < 100) || (a % 100 >= 20 && a % 100 < 100))
- {
- if (a >= 100 && a < 1000 && a % 100 != 0)
- {
- sentence += " and ";
- }
- word = Get20To99AsWord(a % 100);
- if (sentence == "")
- {
- sentence += FirstLetterToUpper(word);
- }
- else
- {
- sentence += word;
- }
- }
- if ((a >= 0 && a <= 9) || (a % 10 >= 0 && a % 10 <= 9))
- {
- if ((a >= 20 && a < 100) || (a % 100 >= 20 && a % 100 < 100))
- {
- sentence += " ";
- }
- else if (a >= 100 && a < 1000 && a % 10 != 0)
- {
- sentence += " and ";
- }
- word = GetDigitAsWord(a % 10);
- if (sentence == "")
- {
- sentence += FirstLetterToUpper(word);
- }
- else
- {
- sentence += word;
- }
- }
- if (a == 0)
- {
- sentence += "Zero";
- }
- return sentence;
- }
- static string GetDigitAsWord(int n)
- {
- string word = "";
- switch (n)
- {
- // case 0:
- // word = "zero";
- // break;
- case 1:
- word = "one";
- break;
- case 2:
- word = "two";
- break;
- case 3:
- word = "three";
- break;
- case 4:
- word = "four";
- break;
- case 5:
- word = "five";
- break;
- case 6:
- word = "six";
- break;
- case 7:
- word = "seven";
- break;
- case 8:
- word = "eight";
- break;
- case 9:
- word = "nine";
- break;
- default:
- word = "";
- break;
- }
- return word;
- }
- static string Get10To19AsWord(int n)
- {
- string word = "";
- switch (n)
- {
- case 10:
- word = "ten";
- break;
- case 11:
- word = "eleven";
- break;
- case 12:
- word = "twelve";
- break;
- case 13:
- word = "thirteen";
- break;
- case 14:
- word = GetDigitAsWord(n % 10) + "teen";
- break;
- case 15:
- word = "fifteen";
- break;
- case 16:
- case 17:
- case 18:
- case 19:
- word = GetDigitAsWord(n % 10) + "teen";
- break;
- default:
- word = "";
- break;
- }
- return word;
- }
- static string Get20To99AsWord(int n)
- {
- string word = "";
- switch (n / 10)
- {
- case 20/10:
- word = "twenty";
- break;
- case 30/10:
- word = "thirty";
- break;
- case 40/10:
- word = GetDigitAsWord(n / 10) + "ty";
- break;
- case 50/10:
- word = "fifty";
- break;
- case 60/10:
- case 70/10:
- word = GetDigitAsWord(n / 10) + "ty";
- break;
- case 80/10:
- word = GetDigitAsWord(n / 10) + "y";
- break;
- case 90/10:
- word = GetDigitAsWord(n / 10) + "ty";
- break;
- default:
- word = "";
- break;
- }
- return word;
- }
- static public string FirstLetterToUpper(string str)
- {
- if (str == null)
- return null;
- if (str.Length > 1)
- return char.ToUpper(str[0]) + str.Substring(1);
- return str.ToUpper();
- }
- // Old answer: This makes every first letter to upper case
- /* http://stackoverflow.com/questions/4135317/make-first-letter-of-a-string-upper-case-for-maximum-performance#4135491 */
- public string ToTitleCase(string str)
- {
- return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
- }
- }
Add Comment
Please, Sign In to add comment