wingman007

NumbersAsWordsWithMethods

Nov 22nd, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3.  
  4. class NumbersAsWords
  5. {
  6.     static void Main()
  7.     {
  8.         for (int i = 0; i < 1000; i++)
  9.         {
  10.             Console.WriteLine("{0,3}: {1}", i, GetNumberAsWords(i));
  11.         }
  12.     }
  13.  
  14.     static string GetNumberAsWords(int a)
  15.     {
  16.         string sentence = "";
  17.         string word = "";
  18.         if (a >= 100 && a < 1000)
  19.         {
  20.             word = GetDigitAsWord(a / 100);
  21.             if (sentence == "")
  22.             {
  23.                 sentence += FirstLetterToUpper(word);
  24.             }
  25.             else
  26.             {
  27.                 sentence += word;
  28.             }          
  29.             sentence += " hundred";
  30.         }
  31.  
  32.         if ((a >= 10 && a < 20) || (a % 100 >= 10 && a % 100 < 20))
  33.         {
  34.             if (a >= 100 && a < 1000 && a % 100 != 0)
  35.             {
  36.                 sentence += " and ";
  37.             }
  38.  
  39.             word = Get10To19AsWord(a % 100);
  40.             if (sentence == "")
  41.             {
  42.                 sentence += FirstLetterToUpper(word);
  43.             }
  44.             else
  45.             {
  46.                 sentence += word;
  47.             }
  48.             return sentence;
  49.         }
  50.  
  51.         if ((a >= 20 && a < 100) || (a % 100 >= 20 && a % 100 < 100))
  52.         {
  53.             if (a >= 100 && a < 1000 && a % 100 != 0)
  54.             {
  55.                 sentence += " and ";
  56.             }
  57.             word = Get20To99AsWord(a % 100);
  58.             if (sentence == "")
  59.             {
  60.                 sentence += FirstLetterToUpper(word);
  61.             }
  62.             else
  63.             {
  64.                 sentence += word;
  65.             }
  66.         }
  67.  
  68.         if ((a >= 0 && a <= 9) || (a % 10 >= 0 && a % 10 <= 9))
  69.         {
  70.             if ((a >= 20 && a < 100) || (a % 100 >= 20 && a % 100 < 100))
  71.             {
  72.                 sentence += " ";
  73.             }
  74.             else if (a >= 100 && a < 1000 && a % 10 != 0)
  75.             {
  76.                 sentence += " and ";
  77.             }
  78.  
  79.             word = GetDigitAsWord(a % 10);
  80.             if (sentence == "")
  81.             {
  82.                 sentence += FirstLetterToUpper(word);
  83.             }
  84.             else
  85.             {
  86.                 sentence += word;
  87.             }
  88.         }
  89.  
  90.         if (a == 0)
  91.         {
  92.             sentence += "Zero";
  93.         }
  94.  
  95.         return sentence;
  96.     }
  97.  
  98.     static string GetDigitAsWord(int n)
  99.     {
  100.         string word = "";
  101.         switch (n)
  102.         {
  103. //            case 0:
  104. //                word = "zero";
  105. //                break;
  106.             case 1:
  107.                 word = "one";
  108.                 break;
  109.             case 2:
  110.                 word = "two";
  111.                 break;
  112.             case 3:
  113.                 word = "three";
  114.                 break;
  115.             case 4:
  116.                 word = "four";
  117.                 break;
  118.             case 5:
  119.                 word = "five";
  120.                 break;
  121.             case 6:
  122.                 word = "six";
  123.                 break;
  124.             case 7:
  125.                 word = "seven";
  126.                 break;
  127.             case 8:
  128.                 word = "eight";
  129.                 break;
  130.             case 9:
  131.                 word = "nine";
  132.                 break;
  133.             default:
  134.                 word = "";
  135.                 break;
  136.         }
  137.         return word;
  138.     }
  139.  
  140.     static string Get10To19AsWord(int n)
  141.     {
  142.         string word = "";
  143.         switch (n)
  144.         {
  145.             case 10:
  146.                 word = "ten";
  147.                 break;
  148.             case 11:
  149.                 word = "eleven";
  150.                 break;
  151.             case 12:
  152.                 word = "twelve";
  153.                 break;
  154.             case 13:
  155.                 word = "thirteen";
  156.                 break;
  157.             case 14:
  158.                 word = GetDigitAsWord(n % 10) + "teen";
  159.                 break;
  160.             case 15:
  161.                 word = "fifteen";
  162.                 break;
  163.             case 16:
  164.             case 17:
  165.             case 18:
  166.             case 19:
  167.                 word = GetDigitAsWord(n % 10) + "teen";
  168.                 break;
  169.             default:
  170.                 word = "";
  171.                 break;
  172.         }
  173.         return word;
  174.     }
  175.  
  176.     static string Get20To99AsWord(int n)
  177.     {
  178.         string word = "";
  179.         switch (n / 10)
  180.         {
  181.             case 20/10:
  182.                 word = "twenty";
  183.                 break;
  184.             case 30/10:
  185.                 word = "thirty";
  186.                 break;
  187.             case 40/10:
  188.                 word = GetDigitAsWord(n / 10) + "ty";
  189.                 break;
  190.             case 50/10:
  191.                 word = "fifty";
  192.                 break;
  193.             case 60/10:
  194.             case 70/10:
  195.                 word = GetDigitAsWord(n / 10) + "ty";
  196.                 break;
  197.             case 80/10:
  198.                 word = GetDigitAsWord(n / 10) + "y";
  199.                 break;
  200.             case 90/10:
  201.                 word = GetDigitAsWord(n / 10) + "ty";
  202.                 break;
  203.             default:
  204.                 word = "";
  205.                 break;
  206.         }
  207.         return word;
  208.     }
  209.  
  210.     static public string FirstLetterToUpper(string str)
  211.     {
  212.         if (str == null)
  213.             return null;
  214.  
  215.         if (str.Length > 1)
  216.             return char.ToUpper(str[0]) + str.Substring(1);
  217.  
  218.         return str.ToUpper();
  219.     }
  220.  
  221.     // Old answer: This makes every first letter to upper case
  222.     /* http://stackoverflow.com/questions/4135317/make-first-letter-of-a-string-upper-case-for-maximum-performance#4135491 */
  223.     public string ToTitleCase(string str)
  224.     {
  225.         return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
  226.     }
  227. }
Add Comment
Please, Sign In to add comment