Convert Number to Words in C# Language

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Globalization;    
  4. using System.Linq;    
  5.     
  6. namespace NumberToText    
  7. {    
  8.     /// <summary>    
  9.     /// Number to word languages    
  10.     /// </summary>    
  11.     public enum Language    
  12.     {    
  13.         /// <summary>    
  14.         /// English Language    
  15.         /// </summary>    
  16.         English,    
  17.         /// <summary>    
  18.         /// Persian Language    
  19.         /// </summary>    
  20.         Persian    
  21.     }    
  22.     /// <summary>    
  23.     /// Digit's groups    
  24.     /// </summary>    
  25.     public enum DigitGroup    
  26.     {    
  27.         /// <summary>    
  28.         /// Ones group    
  29.         /// </summary>    
  30.         Ones,    
  31.         /// <summary>    
  32.         /// Teens group    
  33.         /// </summary>    
  34.         Teens,    
  35.         /// <summary>    
  36.         /// Tens group    
  37.         /// </summary>    
  38.         Tens,    
  39.         /// <summary>    
  40.         /// Hundreds group    
  41.         /// </summary>    
  42.         Hundreds,    
  43.         /// <summary>    
  44.         /// Thousands group    
  45.         /// </summary>    
  46.         Thousands    
  47.     }    
  48.     /// <summary>    
  49.     /// Equivalent names of a group    
  50.     /// </summary>    
  51.     public class NumberWord    
  52.     {    
  53.         /// <summary>    
  54.         /// Digit's group    
  55.         /// </summary>    
  56.         public DigitGroup Group { setget; }    
  57.         /// <summary>    
  58.         /// Number to word language    
  59.         /// </summary>    
  60.         public Language Language { setget; }    
  61.         /// <summary>    
  62.         /// Equivalent names    
  63.         /// </summary>    
  64.         public IList<string> Names { setget; }    
  65.     }    
  66.     /// <summary>    
  67.     /// Convert a number into words    
  68.     /// </summary>    
  69.     public static class ConvertNumberToText    
  70.     {    
  71.         #region Fields (4)    
  72.         /// <summary>    
  73.         ///     
  74.         /// </summary>    
  75.         private static readonly IDictionary<Language, string> And = new Dictionary<Language, string> { { Language.English, " " }, { Language.Persian, " ? " } };    
  76.         /// <summary>    
  77.         ///     
  78.         /// </summary>    
  79.         private static readonly IList<NumberWord> NumberWords = new List<NumberWord>    
  80.         {    
  81.            new NumberWord { Group= DigitGroup.Ones, Language     = Language.English, Names = new List<string> { string.Empty, "One""Two""Three""Four""Five""Six""Seven""Eight""Nine" }},    
  82.            new NumberWord { Group= DigitGroup.Ones, Language     = Language.Persian, Names = new List<string> { string.Empty, "??""??""??""????""???""??""???""???""??" }},    
  83.            new NumberWord { Group= DigitGroup.Teens, Language    = Language.English, Names = new List<string> { "Ten""Eleven""Twelve""Thirteen""Fourteen""Fifteen""Sixteen""Seventeen""Eighteen""Nineteen" }},    
  84.            new NumberWord { Group= DigitGroup.Teens, Language    = Language.Persian, Names = new List<string> { "??""?????""??????""?????""??????""??????""??????""????""????""?????" }},    
  85.            new NumberWord { Group= DigitGroup.Tens, Language     = Language.English, Names = new List<string> { "Twenty""Thirty""Forty""Fifty""Sixty""Seventy""Eighty""Ninety" }},new NumberWord { Group= DigitGroup.Tens, Language= Language.Persian, Names=new List<string> { "????""??""???""?????""???""?????""?????""???" }},    
  86.            new NumberWord { Group= DigitGroup.Hundreds, Language = Language.English, Names = new List<string> {string.Empty, "One Hundred""Two Hundred""Three Hundred""Four Hundred","Five Hundred""Six Hundred""Seven Hundred""Eight Hundred""Nine Hundred" }},new NumberWord { Group= DigitGroup.Hundreds, Language= Language.Persian, Names=new List<string> {string.Empty, "????""?????""????""??????""?????""????""?????""?????" , "????" }},    
  87.            new NumberWord { Group= DigitGroup.Thousands, Language= Language.English, Names = new List<string> { string.Empty, " Thousand"" Million"" Billion"," Trillion"" Quadrillion"" Quintillion"" Sextillian"," Septillion"" Octillion"" Nonillion"" Decillion"" Undecillion"" Duodecillion"" Tredecillion"," Quattuordecillion"" Quindecillion"" Sexdecillion"" Septendecillion"" Octodecillion"" Novemdecillion"," Vigintillion"" Unvigintillion"" Duovigintillion"" 10^72"" 10^75"" 10^78"" 10^81"" 10^84"" 10^87"," Vigintinonillion"" 10^93"" 10^96"" Duotrigintillion"" Trestrigintillion" }},    
  88.            new NumberWord { Group= DigitGroup.Thousands, Language= Language.Persian, Names = new List<string> { string.Empty, " ????"" ??????"" ???????"," ???????"" Quadrillion"" Quintillion"" Sextillian"," Septillion"" Octillion"" Nonillion"" Decillion"" Undecillion"" Duodecillion"" Tredecillion"," Quattuordecillion"" Quindecillion"" Sexdecillion"" Septendecillion"" Octodecillion"" Novemdecillion"," Vigintillion"" Unvigintillion"" Duovigintillion"" 10^72"" 10^75"" 10^78"" 10^81"" 10^84"" 10^87"," Vigintinonillion"" 10^93"" 10^96"" Duotrigintillion"" Trestrigintillion" }},    
  89.         };    
  90.         /// <summary>    
  91.         ///     
  92.         /// </summary>    
  93.         private static readonly IDictionary<Language, string> Negative = new Dictionary<Language, string> { { Language.English, "Negative " }, { Language.Persian, "????? " } };    
  94.         /// <summary>    
  95.         ///     
  96.         /// </summary>    
  97.         private static readonly IDictionary<Language, string> Zero = new Dictionary<Language, string> { { Language.English, "Zero" }, { Language.Persian, "???" } };    
  98.         #endregion Fields    
  99.   
  100.         #region Methods (7)    
  101.     
  102.         // Public Methods (5)    
  103.     
  104.         /// <summary>    
  105.         /// display a numeric value using the equivalent text    
  106.         /// </summary>    
  107.         /// <param name="number">input number</param>    
  108.         /// <param name="language">local language</param>    
  109.         /// <returns>the equivalent text</returns>    
  110.         public static string NumberToText(this int number, Language language)    
  111.         {    
  112.             return NumberToText((long)number, language);    
  113.         }    
  114.         /// <summary>    
  115.         /// display a numeric value using the equivalent text    
  116.         /// </summary>    
  117.         /// <param name="number">input number</param>    
  118.         /// <param name="language">local language</param>    
  119.         /// <returns>the equivalent text</returns>    
  120.         public static string NumberToText(this uint number, Language language)    
  121.         {    
  122.             return NumberToText((long)number, language);    
  123.         }    
  124.         /// <summary>    
  125.         /// display a numeric value using the equivalent text    
  126.         /// </summary>    
  127.         /// <param name="number">input number</param>    
  128.         /// <param name="language">local language</param>    
  129.         /// <returns>the equivalent text</returns>    
  130.         public static string NumberToText(this byte number, Language language)    
  131.         {    
  132.             return NumberToText((long)number, language);    
  133.         }    
  134.         /// <summary>    
  135.         /// display a numeric value using the equivalent text    
  136.         /// </summary>    
  137.         /// <param name="number">input number</param>    
  138.         /// <param name="language">local language</param>    
  139.         /// <returns>the equivalent text</returns>    
  140.         public static string NumberToText(this decimal number, Language language)    
  141.         {    
  142.             return NumberToText((long)number, language);    
  143.         }    
  144.         /// <summary>    
  145.         /// display a numeric value using the equivalent text    
  146.         /// </summary>    
  147.         /// <param name="number">input number</param>    
  148.         /// <param name="language">local language</param>    
  149.         /// <returns>the equivalent text</returns>    
  150.         public static string NumberToText(this double number, Language language)    
  151.         {    
  152.             return NumberToText((long)number, language);    
  153.         }    
  154.         /// <summary>    
  155.         /// display a numeric value using the equivalent text    
  156.         /// </summary>    
  157.         /// <param name="number">input number</param>    
  158.         /// <param name="language">local language</param>    
  159.         /// <returns>the equivalent text</returns>    
  160.         public static string NumberToText(this long number, Language language)    
  161.         {    
  162.             //    
  163.             if (number == 0) { return Zero[language]; }    
  164.             //    
  165.             if (number < 0) { return Negative[language] + NumberToText(-number, language); }    
  166.             //    
  167.             return Wordify(number, language, string.Empty, 0);    
  168.         }    
  169.         /// <summary>    
  170.         ///     
  171.         /// </summary>    
  172.         /// <param name="value">input string number</param>    
  173.         /// <param name="language">local language</param>    
  174.         /// <returns></returns>    
  175.         public static string NumberToText(this string value, Language language)    
  176.         {    
  177.             int number;    
  178.             if (int.TryParse(value, out number)) return NumberToText(double.Parse(value), language);    
  179.             throw new FormatException("Input string is not correct format.");    
  180.         }    
  181.         // Private Methods (2)    
  182.     
  183.         /// <summary>    
  184.         ///     
  185.         /// </summary>    
  186.         /// <param name="idx"></param>    
  187.         /// <param name="language"></param>    
  188.         /// <param name="group"></param>    
  189.         /// <returns></returns>    
  190.         private static string GetName(int idx, Language language, DigitGroup group)    
  191.         {    
  192.             return NumberWords.First(x => x.Group == group && x.Language == language).Names[idx];    
  193.         }    
  194.         /// <summary>    
  195.         ///     
  196.         /// </summary>    
  197.         /// <param name="number"></param>    
  198.         /// <param name="language"></param>    
  199.         /// <param name="leftDigitsText"></param>    
  200.         /// <param name="thousands"></param>    
  201.         /// <returns></returns>    
  202.         private static string Wordify(long number, Language language, string leftDigitsText, int thousands)    
  203.         {    
  204.             //    
  205.             if (number == 0) return leftDigitsText;    
  206.             //    
  207.             var wordValue = leftDigitsText;    
  208.             //    
  209.             if (wordValue.Length > 0) wordValue += And[language];    
  210.             //    
  211.             if (number < 10) wordValue += GetName((int)number, language, DigitGroup.Ones);    
  212.             //    
  213.             else if (number < 20) wordValue += GetName((int)(number - 10), language, DigitGroup.Teens);    
  214.             //    
  215.             else if (number < 100) wordValue += Wordify(number % 10, language, GetName((int)(number / 10 - 2), language, DigitGroup.Tens), 0);    
  216.             //    
  217.             else if (number < 1000) wordValue += Wordify(number % 100, language, GetName((int)(number / 100), language, DigitGroup.Hundreds), 0);    
  218.             //    
  219.             else wordValue += Wordify(number % 1000, language, Wordify(number / 1000, language, string.Empty, thousands + 1), 0);    
  220.             //    
  221.             if (number % 1000 == 0) return wordValue;    
  222.             //    
  223.             return wordValue + GetName(thousands, language, DigitGroup.Thousands);    
  224.         }    
  225.         #endregion Methods    
  226.     }    
  227. }   
Example:
  1. using System;  
  2.   
  3. namespace NumberToText  
  4. {  
  5.     static class Program  
  6.     {  
  7.         static void Main()  
  8.         {  
  9.             Console.WriteLine("1223".NumberToText(Language.English));  
  10.             var num = 10;  
  11.             Console.WriteLine(num.NumberToText(Language.English));  
  12.         }  
  13.     }  

For download sample project click the following link
 https://github.com/omidnasri/NumberToText