KlimentHristov

**NumbersAsWords

Nov 23rd, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2. class NumbersAsWords
  3. {
  4.     static void Main()
  5.     {
  6.         string input;
  7.         int number;
  8.         bool isValid;
  9.         bool isUK = false;
  10.  
  11.         do
  12.         {
  13.             Console.Write("Enter integer : ");
  14.             input = Console.ReadLine();
  15.             isValid = int.TryParse(input, out number);
  16.             if (!isValid)
  17.                 Console.WriteLine("\n  Not an integer, please try again\n");
  18.             else
  19.                 Console.WriteLine("\n  {0}\n", NumberToText(number, isUK));
  20.         }
  21.         while (!(isValid && number == 0));
  22.         Console.WriteLine("\nProgram ended");
  23.     }
  24.     public static string NumberToText(int number, bool isUK)
  25.     {
  26.         if (number == 0) return "Zero";
  27.         string and = isUK ? "and " : ""; // deals with UK or US numbering
  28.         if (number == -2147483648) return "Minus Two Billion One Hundred " + and +
  29.         "Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
  30.         "Six Hundred " + and + "Forty Eight";
  31.         int[] num = new int[4];
  32.         int first = 0;
  33.         int u, h, t;
  34.         System.Text.StringBuilder sb = new System.Text.StringBuilder();
  35.         if (number < 0)
  36.         {
  37.             sb.Append("Minus ");
  38.             number = -number;
  39.         }
  40.         string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
  41.         string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
  42.         string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
  43.         string[] words3 = { "Thousand ", "Million ", "Billion " };
  44.         num[0] = number % 1000;           // units
  45.         num[1] = number / 1000;
  46.         num[2] = number / 1000000;
  47.         num[1] = num[1] - 1000 * num[2];  // thousands
  48.         num[3] = number / 1000000000;     // billions
  49.         num[2] = num[2] - 1000 * num[3];  // millions
  50.         for (int i = 3; i > 0; i--)
  51.         {
  52.             if (num[i] != 0)
  53.             {
  54.                 first = i;
  55.                 break;
  56.             }
  57.         }
  58.         for (int i = first; i >= 0; i--)
  59.         {
  60.             if (num[i] == 0) continue;
  61.             u = num[i] % 10;              // ones
  62.             t = num[i] / 10;
  63.             h = num[i] / 100;             // hundreds
  64.             t = t - 10 * h;               // tens
  65.             if (h > 0) sb.Append(words0[h] + "Hundred ");
  66.             if (u > 0 || t > 0)
  67.             {
  68.                 if (h > 0 || i < first) sb.Append(and);
  69.                 if (t == 0)
  70.                     sb.Append(words0[u]);
  71.                 else if (t == 1)
  72.                     sb.Append(words1[u]);
  73.                 else
  74.                     sb.Append(words2[t - 2] + words0[u]);
  75.             }
  76.             if (i != 0) sb.Append(words3[i - 1]);
  77.         }
  78.         return sb.ToString().TrimEnd();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment