Software & Finance





C# - Convert Numbers to Text (Words)





Here is the C# program for converting numbers into words. The logic here is to reuse the same string as much as possible. If you look at the string array in the code, I have defined strones and strtens. Once we have that, we can parse the number and get the corresponding string and appending it to come up with the result.


I have given here the code to display the numbers upto 100,000. If you want more limit, you can keep extending the program. 

 

Here are other useful links where you can find the same program in other programming languages:

 


 

Source Code


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CSN2W

{

   class Program

   {

      static bool HelperConvertNumberToText(int num, out string buf)

      {

          string [] strones = {

            "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",

            "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",

            "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",

          };

       

          string [] strtens = {

              "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",

              "Seventy", "Eighty", "Ninety", "Hundred"

          };

 

          string result = "";

          buf = "";

          int single, tens, hundreds;

       

          if(num > 1000)

              return false;

       

          hundreds = num / 100;

          num = num - hundreds * 100;

          if( num < 20)

          {

              tens = 0; // special case

              single = num;

          }

          else

          {

              tens = num / 10;

              num = num - tens * 10;

              single = num;

          }

 

          result = "";

         

          if(hundreds > 0)

          {

             result += strones[hundreds-1];

             result += " Hundred ";

          }

          if(tens > 0)

          {

             result += strtens[tens - 1];

             result += " ";

          }

          if(single > 0)

          {

             result += strones[single - 1];

             result += " ";

          }

 

          buf = result;

          return true;

      }

 

      static bool ConvertNumberToText(int num, out string result)

      {

          string tempString = "";

          int thousands;

          int temp;

          result = "";

          if(num < 0 || num > 100000)

          {

              System.Console.WriteLine(num + " \tNot Supported");

              return false;

          }

       

          if( num == 0)

          {

             System.Console.WriteLine(num + " \tZero");

             return false;

          }

               

          if(num < 1000)

          {  

              HelperConvertNumberToText(num, out tempString);

              result += tempString;

          }

          else

          {

              thousands = num / 1000;

              temp = num - thousands * 1000;

              HelperConvertNumberToText(thousands, out tempString);

              result += tempString;

              result += "Thousand ";             

              HelperConvertNumberToText(temp, out tempString);

              result += tempString;

          }

          return true;

      }

 

      static void Main(string[] args)

      {

          string result;

          int  i, num;

          int [] arrNum =

          {

            -1, 0, 5, 10, 15, 19, 20, 21, 25, 33, 49, 50, 72,

            99, 100, 101, 117, 199, 200, 214, 517, 589, 999,

            1000, 1010, 1018, 1200, 9890, 10119, 13535, 57019,

            99999, 100000, 100001

          };

 

          for (i = 0; i < arrNum.Count(); i++)

          {

              num = arrNum[i];

              if( ConvertNumberToText(num, out result) == true)

                 Console.WriteLine(num  + "\t" + result);

          }

      }

   }

}

Output


-1       Not Supported

0        Zero

5        Five

10       Ten

15       Fifteen

19       Nineteen

20       Twenty

21       Twenty One

25       Twenty Five

33       Thirty Three

49       Fourty Nine

50       Fifty

72       Seventy Two

99       Ninety Nine

100      One Hundred

101      One Hundred One

117      One Hundred Seventeen

199      One Hundred Ninety Nine

200      Two Hundred

214      Two Hundred Fourteen

517      Five Hundred Seventeen

589      Five Hundred Eighty Nine

999      Nine Hundred Ninety Nine

1000     One Thousand

1010     One Thousand Ten

1018     One Thousand Eighteen

1200     One Thousand Two Hundred

9890     Nine Thousand Eight Hundred Ninety

10119    Ten Thousand One Hundred Nineteen

13535    Thirteen Thousand Five Hundred Thirty Five

57019    Fifty Seven Thousand Nineteen

99999    Ninety Nine Thousand Nine Hundred Ninety Nine

100000   One Hundred Thousand

100001   Not Supported