Convert Numbers To Words In C#

Here, I have described how to convert numeric values to words -  not only whole numbers but also, the numbers with decimal values. For example,
 
562.53 = Five Hundred And Sixty Two Point Fifty Three Only.
 
The function is named "ConvertAmount" and accepts a parameter of type double. First, the function checks the existence of decimal in the number passed as a parameter.

If decimal exists, it constructs the word for point value. After removing the decimal digits from the number, it converts digits, tens, and hundreds by using the function "Convert" and returns a string containing the number in words.
  1. class NumberToWords  
  2. {  
  3.     private static String[] units = { "Zero""One""Two""Three",  
  4.     "Four""Five""Six""Seven""Eight""Nine""Ten""Eleven",  
  5.     "Twelve""Thirteen""Fourteen""Fifteen""Sixteen",  
  6.     "Seventeen""Eighteen""Nineteen" };  
  7.     private static String[] tens = { """""Twenty""Thirty""Forty",  
  8.     "Fifty""Sixty""Seventy""Eighty""Ninety" };  
  9.   
  10.     public static String ConvertAmount(double amount)  
  11.     {  
  12.         try  
  13.         {  
  14.             Int64 amount_int = (Int64)amount;  
  15.             Int64 amount_dec = (Int64)Math.Round((amount - (double)(amount_int)) * 100);  
  16.             if (amount_dec == 0)  
  17.             {  
  18.                 return Convert(amount_int) + " Only.";  
  19.             }  
  20.             else  
  21.             {  
  22.                 return Convert(amount_int) + " Point " + Convert(amount_dec) + " Only.";  
  23.             }  
  24.         }  
  25.         catch (Exception e)  
  26.         {  
  27.             // TODO: handle exception  
  28.         }  
  29.         return "";  
  30.     }  
  31.   
  32.     public static String Convert(Int64 i)  
  33.     {  
  34.         if (i < 20)  
  35.         {  
  36.             return units[i];  
  37.         }  
  38.         if (i < 100)  
  39.         {  
  40.             return tens[i / 10] + ((i % 10 > 0) ? " " + Convert(i % 10) : "");  
  41.         }  
  42.         if (i < 1000)  
  43.         {  
  44.             return units[i / 100] + " Hundred"  
  45.                     + ((i % 100 > 0) ? " And " + Convert(i % 100) : "");  
  46.         }  
  47.         if (i < 100000)  
  48.         {           return Convert(i / 1000) + " Thousand "  
  49.                     + ((i % 1000 > 0) ? " " + Convert(i % 1000) : "");  
  50.         }  
  51.         if (i < 10000000)  
  52.         {  
  53.             return Convert(i / 100000) + " Lakh "  
  54.                     + ((i % 100000 > 0) ? " " + Convert(i % 100000) : "");  
  55.         }  
  56.         if (i < 1000000000)  
  57.         {  
  58.             return Convert(i / 10000000) + " Crore "  
  59.                     + ((i % 10000000 > 0) ? " " + Convert(i % 10000000) : "");  
  60.         }  
  61.         return Convert(i / 1000000000) + " Arab "  
  62.                 + ((i % 1000000000 > 0) ? " " + Convert(i % 1000000000) : "");  
  63.     }  

Now, call the ConvertAmount method.
  1. static void Main(string[] args)  
  2. {  
  3.     try  
  4.     {  
  5.         Console.WriteLine("Enter a Number to convert to words");  
  6.         string number = Console.ReadLine();  
  7.         number = ConvertAmount(double.Parse(number));  
  8.   
  9.         Console.WriteLine("Number in words is \n{0}", number);  
  10.         Console.ReadKey();  
  11.     }  
  12.     catch (Exception ex)  
  13.     {  
  14.         Console.WriteLine(ex.Message);  
  15.     }  
  16. }
I hope this is useful for all readers. Happy Coding!