Converting a number into its text presentation

I wrote this code 2 years ago, it is an interesting piece of code to convert any number between 0 and 999,999,999 into its text presentation.

Just create a console application and paste this code :

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4:  
   5: namespace NumberConverter
   6: {
   7:     class Program
   8:     {
   9:         public static string ConvertNumber(uint Number)
  10:         {
  11:             string strhh, strh1, strh2, strh3;
  12:             string str = "";
  13:             uint h1 = Number / 100; //hundreds
  14:             uint h2 = Number % 100;
  15:             uint h3 = h2 / 10; //tens
  16:             uint h4 = h2 % 10; //units
  17:  
  18:             switch (h1)
  19:             {
  20:                 case 1:
  21:                     strh3 = "one hundred";
  22:                     break;
  23:                 case 2:
  24:                     strh3 = "two hundred";
  25:                     break;
  26:                 case 3:
  27:                     strh3 = "three hundred";
  28:                     break;
  29:                 case 4:
  30:                     strh3 = "four hundred";
  31:                     break;
  32:                 case 5:
  33:                     strh3 = "five hundred";
  34:                     break;
  35:                 case 6:
  36:                     strh3 = "six hundred";
  37:                     break;
  38:                 case 7:
  39:                     strh3 = "seven hundred";
  40:                     break;
  41:                 case 8:
  42:                     strh3 = "eight hundred";
  43:                     break;
  44:                 case 9:
  45:                     strh3 = "nine hundred";
  46:                     break;
  47:                 default:
  48:                     strh3 = "";
  49:                     break;
  50:             }
  51:             switch (h3)
  52:             {
  53:                 case 1:
  54:                     strh2 = "ten";
  55:                     break;
  56:                 case 2:
  57:                     strh2 = "twenty";
  58:                     break;
  59:                 case 3:
  60:                     strh2 = "thirty";
  61:                     break;
  62:                 case 4:
  63:                     strh2 = "fourty";
  64:                     break;
  65:                 case 5:
  66:                     strh2 = "fifty";
  67:                     break;
  68:                 case 6:
  69:                     strh2 = "sixty";
  70:                     break;
  71:                 case 7:
  72:                     strh2 = "seventy";
  73:                     break;
  74:                 case 8:
  75:                     strh2 = "eighty";
  76:                     break;
  77:                 case 9:
  78:                     strh2 = "ninety";
  79:                     break;
  80:                 default:
  81:                     strh2 = "";
  82:                     break;
  83:             }
  84:  
  85:             switch (h4)
  86:             {
  87:                 case 1:
  88:                     strh1 = "one";
  89:                     break;
  90:                 case 2:
  91:                     strh1 = "two";
  92:                     break;
  93:                 case 3:
  94:                     strh1 = "three";
  95:                     break;
  96:                 case 4:
  97:                     strh1 = "four";
  98:                     break;
  99:                 case 5:
 100:                     strh1 = "five";
 101:                     break;
 102:                 case 6:
 103:                     strh1 = "six";
 104:                     break;
 105:                 case 7:
 106:                     strh1 = "seven";
 107:                     break;
 108:                 case 8:
 109:                     strh1 = "eight";
 110:                     break;
 111:                 case 9:
 112:                     strh1 = "nine";
 113:                     break;
 114:                 default:
 115:                     strh1 = "zero";
 116:                     break;
 117:             }
 118:  
 119:             //Eleven - Twelve - ... - ninetee
 120:             if (strh2 == "ten" && strh1 == "one")
 121:             {
 122:                 strh1 = "";
 123:                 strh2 = "eleven";
 124:             }
 125:             else if (strh2 == "ten" && strh1 == "two")
 126:             {
 127:                 strh1 = "";
 128:                 strh2 = "twelve";
 129:  
 130:             }
 131:             else if (strh2 == "ten" && strh1 == "three")
 132:             {
 133:                 strh1 = "";
 134:                 strh2 = "thirteen";
 135:  
 136:             }
 137:             else if (strh2 == "ten" && strh1 == "four")
 138:             {
 139:                 strh1 = "";
 140:                 strh2 = "fourteen";
 141:  
 142:             }
 143:             else if (strh2 == "ten" && strh1 == "five")
 144:             {
 145:                 strh1 = "";
 146:                 strh2 = "fifteen";
 147:  
 148:             }
 149:             else if (strh2 == "ten" && strh1 == "six")
 150:             {
 151:                 strh1 = "";
 152:                 strh2 = "sixteen";
 153:  
 154:             }
 155:             else if (strh2 == "ten" && strh1 == "seven")
 156:             {
 157:                 strh1 = "";
 158:                 strh2 = "seventeen";
 159:  
 160:             }
 161:             else if (strh2 == "ten" && strh1 == "eight")
 162:             {
 163:                 strh1 = "";
 164:                 strh2 = "eighteen";
 165:  
 166:             }
 167:             else if (strh2 == "ten" && strh1 == "nine")
 168:             {
 169:                 strh1 = "";
 170:                 strh2 = "nineteen";
 171:  
 172:             }
 173:  
 174:             //special cases
 175:             if (str.Length == 1)
 176:             {
 177:                 strhh = strh1;
 178:             }
 179:  
 180:  
 181:             else if (strh1 == "zero")
 182:             {
 183:                 strhh = strh3 + " " + strh2;
 184:  
 185:             }
 186:             else if (strh2 == "")
 187:             {
 188:                 strhh = strh3 + " " + strh2 + strh1;
 189:             }
 190:  
 191:             else if (str.Length == 2)
 192:             {
 193:                 strhh = strh3 + " " + strh2 + " " + strh1;
 194:             }
 195:             else
 196:             {
 197:                 strhh = strh3 + " " + strh2 + " " + strh1;
 198:  
 199:             }
 200:             return strhh;
 201:  
 202:  
 203:         }
 204:         static void Main(string[] args)
 205:         {
 206:         CaseNotNumber:
 207:  
 208:             try
 209:             {
 210:                 Console.Write("Enter a number to convert it to a Text, 0 to exit: ");
 211:                 uint Number = uint.Parse(Console.ReadLine());
 212:                 if (Number > 0)
 213:                 {
 214:  
 215:  
 216:                     string str = Number.ToString();
 217:  
 218:                     //representing hundreds 
 219:                     if (str.Length <= 3)
 220:                     {
 221:                         string result = ConvertNumber(Number);
 222:                         Console.WriteLine("");
 223:                         Console.WriteLine("Result");
 224:                         Console.WriteLine("------");
 225:                         Console.WriteLine("Input: {0}\n", Number);
 226:                         Console.WriteLine("Output: {0}", result);
 227:                         Console.ReadLine();
 228:  
 229:                     }
 230:                     //representing thousands
 231:                     if (str.Length <= 6 && str.Length > 3)
 232:                     {
 233:                         //pattern goes here
 234:                         string result;
 235:                         string subResult1;
 236:                         string subResult2;
 237:                         string str1 = str.Substring(0, str.Length - 3);//thousands
 238:                         uint Num1 = uint.Parse(str1);
 239:                         string str2 = str.Substring(str.Length - 3, 3);//tens
 240:                         uint Num2 = uint.Parse(str2);
 241:                         subResult1 = ConvertNumber(Num1);
 242:                         subResult2 = ConvertNumber(Num2);
 243:                         result = subResult1 + " " + "thousand" + "," + " " + subResult2;
 244:                         Console.WriteLine("");
 245:                         Console.WriteLine("Result");
 246:                         Console.WriteLine("------");
 247:                         Console.WriteLine("Input: {0}\n", Number);
 248:                         Console.WriteLine("Output: {0}", result);
 249:                         Console.ReadLine();
 250:  
 251:                     }
 252:  
 253:                     //representing millions
 254:                     if (str.Length <= 9 && str.Length > 6)
 255:                     {
 256:                         string result;
 257:                         string subResult1;
 258:                         string subResult2;
 259:                         string subResult3;
 260:                         string str1 = str.Substring(0, str.Length - 6);//millions
 261:                         uint Num1 = uint.Parse(str1);
 262:                         string str2 = str.Substring(str.Length - 6, 3);//thousands
 263:                         uint Num2 = uint.Parse(str2);
 264:                         string str3 = str.Substring(str1.Length + 3, 3);//tens
 265:                         uint Num3 = uint.Parse(str3);
 266:                         subResult1 = ConvertNumber(Num1);
 267:                         subResult2 = ConvertNumber(Num2);
 268:                         subResult3 = ConvertNumber(Num3);
 269:                         if (Num2 == 0)
 270:                         {
 271:                             result = subResult1 + " " + "million" + "," + subResult2 + " " + subResult3;
 272:                         }
 273:                         else
 274:                         {
 275:                             result = subResult1 + " " + "million" + "," + " " + subResult2 + " " + "thousand" + "," + " " + subResult3;
 276:                         }
 277:                         Console.WriteLine("");
 278:                         Console.WriteLine("Result");
 279:                         Console.WriteLine("------");
 280:                         Console.WriteLine("Input: {0}\n", Number);
 281:                         Console.WriteLine("Output: {0}", result);
 282:                         Console.ReadLine();
 283:  
 284:                     }
 285:  
 286:                 }
 287:  
 288:             }
 289:             catch (Exception ex)
 290:             {
 291:                 Console.WriteLine();
 292:                 Console.WriteLine("Error: only positive numeric values ( 0 - 999,999,999) are allowed, press enter to try again");
 293:                 Console.WriteLine();
 294:                 Console.ReadLine();
 295:                 goto CaseNotNumber;
 296:             }
 297:  
 298:         }
 299:     }
 300: }

 

Enjoy it!

Published Monday, January 28, 2008 11:09 PM by Joseph Ghassan
Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)