Convert Numeric Number to String in C#

Hi All,

This program is returning output as string, lets go to an example. Such as, if you enter a number 19, then this program will return "Nineteen". So shall we do it????

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConvertNumericToString  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.            do  
  13.             {  
  14.                 Console.WriteLine("Please enter the number..");  
  15.                 int intNum = Convert.ToInt32(Console.ReadLine());  
  16.                 Console.WriteLine(CheckAndDisplayString(intNum));  
  17.                 Console.WriteLine("Do you want to continue ..Y/N?");  
  18.   
  19.             } while (Console.ReadLine().ToLower() == "y");  
  20.             Console.ReadLine();  
  21.         }  
  22.   
  23.         private static string CheckAndDisplayString(int intNum)  
  24.         {  
  25.             try  
  26.             {  
  27.                 var str1To19 = new string[] { "zero""one""two",   
  28.                 "three""four""five""six",   
  29.                 "seven""eight""nine""ten",   
  30.                 "eleven""twelve",  "thirteen",   
  31.                 "fourteen""fifteen""sixteen",  "seventeen",   
  32.                 "eighteen""nineteen"};  
  33.                 var strMultipleOfTen = new string[] { "twenty""thirty",   
  34.                 "forty""fifty""sixty""seventy",   
  35.                 "eighty""ninety" };  
  36.                 //If the Number is zero return null  
  37.                 if (intNum == 0)  
  38.                     return "zero";  
  39.                 //check the 100 digit  
  40.                 string strResult = "";  
  41.                 int intDigit;  
  42.                 if (intNum > 100)  
  43.                 {  
  44.                     intDigit = intNum / 100;  
  45.                     intNum = intNum % 100;  
  46.                     strResult = str1To19[intDigit] + "hundred";  
  47.                 }  
  48.                 if (intNum == 0)  
  49.                     return strResult.Trim();  
  50.                 if (intNum < 20)  
  51.                     strResult += " " + str1To19[intNum];  
  52.                 else  
  53.                 {  
  54.                     //handles 10 digit  
  55.                     intDigit = intNum / 10;  
  56.                     intNum = intNum % 10;  
  57.                     strResult += " " + strMultipleOfTen[intDigit - 2];  
  58.                     if (intNum > 0)  
  59.                         strResult += " " + str1To19[intNum];  
  60.   
  61.                 }  
  62.                 return strResult;  
  63.             }  
  64.             catch  
  65.             {  
  66.                 return "The value you entered is not applicable..";  
  67.             }  
  68.         }  
  69.     }  
  70. }  
See the output here:

output

Thank you :)

 

Next Recommended Reading Convert String to ASCII in C#