Convert Numeric Value Into Words (Currency) In C#

Introduction

In some situations, we will get a requirement like converting a number into words.

Example

568 = Five Hundred Sixty Eight only.

Here, I will explain how to convert this numeric value to words (In Indian Currency format), using C#, not only whole numbers but also with decimal and negative values.

Example

568.25 = Five Hundred Sixty Eight and two five paisa only.

-25 = Minus twenty five only.

Explanation

  1. Let’s begin converting numeric value into words. Before writing code, let's analyze how can we convert numeric values to words. For this, let’s take a 4 digit number (4568).



    Now, we can start with ones. First, I am going to write a function for ones. This function should accept the numeric string and should return a converted word function, as shown below.
    1. private static String ones(String Number)  
    2.         {  
    3.             int _Number = Convert.ToInt32(Number);  
    4.             String name = "";  
    5.             switch (_Number)  
    6.             {  
    7.               
    8.                 case 1:  
    9.                     name = "One";  
    10.                     break;  
    11.                 case 2:  
    12.                     name = "Two";  
    13.                     break;  
    14.                 case 3:  
    15.                     name = "Three";  
    16.                     break;  
    17.                 case 4:  
    18.                     name = "Four";  
    19.                     break;  
    20.                 case 5:  
    21.                     name = "Five";  
    22.                     break;  
    23.                 case 6:  
    24.                     name = "Six";  
    25.                     break;  
    26.                 case 7:  
    27.                     name = "Seven";  
    28.                     break;  
    29.                 case 8:  
    30.                     name = "Eight";  
    31.                     break;  
    32.                 case 9:  
    33.                     name = "Nine";  
    34.                     break;  
    35.             }  
    36.             return name;  
    37.    }   

    As you can see, the function given above accepts one string, that is our numeric one’s position value and here, I declared it as a string name for saving word, which is based on number you passed. I used a switch case to check all one's position value and save word, based on the number. This function will work only on single digit (1 to 9).

  1. Next step is to write a function, which is also same and it accepts one numeric string and returns converted word string. Here we can pass two digit value, the function as shown below.
    1. private static String tens(String Number)  
    2.         {  
    3.             int _Number = Convert.ToInt32(Number);  
    4.             String name = null;  
    5.             switch (_Number)  
    6.             {  
    7.                 case 10:  
    8.                     name = "Ten";  
    9.                     break;  
    10.                 case 11:  
    11.                     name = "Eleven";  
    12.                     break;  
    13.                 case 12:  
    14.                     name = "Twelve";  
    15.                     break;  
    16.                 case 13:  
    17.                     name = "Thirteen";  
    18.                     break;  
    19.                 case 14:  
    20.                     name = "Fourteen";  
    21.                     break;  
    22.                 case 15:  
    23.                     name = "Fifteen";  
    24.                     break;  
    25.                 case 16:  
    26.                     name = "Sixteen";  
    27.                     break;  
    28.                 case 17:  
    29.                     name = "Seventeen";  
    30.                     break;  
    31.                 case 18:  
    32.                     name = "Eighteen";  
    33.                     break;  
    34.                 case 19:  
    35.                     name = "Nineteen";  
    36.                     break;  
    37.                 case 20:  
    38.                     name = "Twenty";  
    39.                     break;  
    40.                 case 30:  
    41.                     name = "Thirty";  
    42.                     break;  
    43.                 case 40:  
    44.                     name = "Fourty";  
    45.                     break;  
    46.                 case 50:  
    47.                     name = "Fifty";  
    48.                     break;  
    49.                 case 60:  
    50.                     name = "Sixty";  
    51.                     break;  
    52.                 case 70:  
    53.                     name = "Seventy";  
    54.                     break;  
    55.                 case 80:  
    56.                     name = "Eighty";  
    57.                     break;  
    58.                 case 90:  
    59.                     name = "Ninety";  
    60.                     break;  
    61.                 default:  
    62.                     if (_Number > 0)  
    63.                     {  
    64.                         name = tens(Number.Substring(0, 1) + "0") + " " + ones(Number.Substring(1));  
    65.                     }  
    66.                     break;  
    67.             }  
    68.             return name;  
    69.         }   

    This function is also the same as previous functions but here, you can pass a two-digit number. First the number will pass to this function for example, 25.

    Now, this 25 will be checked in switch case, if it's matching, then it returns based on that number name string, else it goes to default. Here, it goes to default.

    In this default, I am checking the number is greater than zero, if zero, then I am recursively calling this function again but before that I am removing the last number (here 5) and concatenating zero to it i.e. you will get as 20. I am passing this 20 to this function again, so that I will get the result. Here, I will get “Twenty” as a word then again I am concatenating space and I am calling one's function, which I have created in the step 1.in this function. I am passing last digit, which is 5, then we will get word as “Five” i.e. we will get the result as “Twenty Five”

  1. So far, we have seen how to convert the two-digit number into words. If the number contains more than two digits, how will we convert it? For this, I am going to write a function, which accepts a number up to 12 digits as a string and returns as a converted word string. The function is shown below.
      1. private static String ConvertWholeNumber(String Number)  
      2.       {  
      3.           string word = "";  
      4.           try  
      5.           {  
      6.               bool beginsZero = false;//tests for 0XX  
      7.               bool isDone = false;//test if already translated  
      8.               double dblAmt = (Convert.ToDouble(Number));  
      9.               //if ((dblAmt > 0) && number.StartsWith("0"))  
      10.               if (dblAmt > 0)  
      11.               {//test for zero or digit zero in a nuemric  
      12.                   beginsZero = Number.StartsWith("0");  
      13.   
      14.                   int numDigits = Number.Length;  
      15.                   int pos = 0;//store digit grouping  
      16.                   String place = "";//digit grouping name:hundres,thousand,etc...  
      17.                   switch (numDigits)  
      18.                   {  
      19.                       case 1://ones' range  
      20.                            
      21.                           word = ones(Number);  
      22.                           isDone = true;  
      23.                           break;  
      24.                       case 2://tens' range  
      25.                           word = tens(Number);  
      26.                           isDone = true;  
      27.                           break;  
      28.                       case 3://hundreds' range  
      29.                           pos = (numDigits % 3) + 1;  
      30.                           place = " Hundred ";  
      31.                           break;  
      32.                       case 4://thousands' range  
      33.                       case 5:  
      34.                       case 6:  
      35.                           pos = (numDigits % 4) + 1;  
      36.                           place = " Thousand ";  
      37.                           break;  
      38.                       case 7://millions' range  
      39.                       case 8:  
      40.                       case 9:  
      41.                           pos = (numDigits % 7) + 1;  
      42.                           place = " Million ";  
      43.                           break;  
      44.                       case 10://Billions's range  
      45.                       case 11:  
      46.                       case 12:  
      47.                         
      48.                           pos = (numDigits % 10) + 1;  
      49.                           place = " Billion ";  
      50.                           break;  
      51.                       //add extra case options for anything above Billion...  
      52.                       default:  
      53.                           isDone = true;  
      54.                           break;  
      55.                   }                     
      56.                   if (!isDone)  
      57.                   {//if transalation is not done, continue...(Recursion comes in now!!)  
      58.                       if (Number.Substring(0, pos) != "0" && Number.Substring(pos) != "0")  
      59.                       {  
      60.                           try  
      61.                           {  
      62.                               word = ConvertWholeNumber(Number.Substring(0, pos)) + place + ConvertWholeNumber(Number.Substring(pos));  
      63.                           }  
      64.                           catch { }  
      65.                       }  
      66.                       else  
      67.                       {  
      68.                           word = ConvertWholeNumber(Number.Substring(0, pos))  + ConvertWholeNumber(Number.Substring(pos));  
      69.                       }  
      70.                       
      71.                       //check for trailing zeros  
      72.                      //if (beginsZero) word = " and " + word.Trim();  
      73.                   }  
      74.                   //ignore digit grouping names  
      75.                   if (word.Trim().Equals(place.Trim())) word = "";  
      76.               }  
      77.           }  
      78.           catch { }  
      79.           return word.Trim();  
      80.       } 

      Here, as you can see this function is also same as the previous one buta  little bit changes here. In this, we switch case. I am checking up to 12 digits due to which there are 12 cases, if you know more than that case, then you can have  12 cases. Here, if you pass more than 12 digits, it will return empty as a word because by default, I am updating isDone Boolean to true, then it will return nothing. Here, if you pass a single digit, then it will go to case one and calls step1 function ones and it returns and I am updating isDone Boolean to true, so it can return from this function as well and for two-digit numbers, it is also  the same but for cases, where there are more than two digits, I didn’t update isDone Boolean. Thus, after executing that case, it comes to if condition. Here, I am recursively calling this function again which is based on the position, until isDone Boolean updates to true.

      For example I am passing the number 4568, then in this function, case 4 will execute first because it has 4 digits and in case 4, you will get pos as (4568%4) +1 =0+1=1, place =” Thousand”

      It comes to if condition and recursively calls it, which is based on this position.

      Thus, it calls for substring from 0 to pos i.e. 1 character -- that is, you will get 4.

      Thus, it returns “Four” + “Thousand”+ again. 

      At last, we will get an output as “Four Thousand Five Hundred Sixty Eight”.

      This is how we can convert the whole number with 12 digit convert to words.

    1. The next step is to check if any decimal values are there, convert this decimal value separately and attach to this word. The functions are shown below.
      1. private static String ConvertToWords(String numb)  
      2.         {  
      3.             String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";  
      4.             String endStr = "Only";  
      5.             try  
      6.             {  
      7.                 int decimalPlace = numb.IndexOf(".");  
      8.                 if (decimalPlace > 0)  
      9.                 {  
      10.                     wholeNo = numb.Substring(0, decimalPlace);  
      11.                     points = numb.Substring(decimalPlace + 1);  
      12.                     if (Convert.ToInt32(points) > 0)  
      13.                     {  
      14.                         andStr = "and";// just to separate whole numbers from points/cents  
      15.                         endStr = "Paisa " + endStr;//Cents  
      16.                         pointStr = ConvertDecimals(points);  
      17.                     }  
      18.                 }  
      19.                 val = String.Format("{0} {1}{2} {3}", ConvertWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);  
      20.             }  
      21.             catch { }  
      22.             return val;  
      23.         }   

      To convert decimal numbers to words, the function is shown below. 

      1. private static String ConvertDecimals(String number)  
      2.         {  
      3.             String cd = "", digit = "", engOne = "";  
      4.             for (int i = 0; i < number.Length; i++)  
      5.             {  
      6.                 digit = number[i].ToString();  
      7.                 if (digit.Equals("0"))  
      8.                 {  
      9.                     engOne = "Zero";  
      10.                 }  
      11.                 else  
      12.                 {  
      13.                     engOne = ones(digit);  
      14.                 }  
      15.                 cd += " " + engOne;  
      16.             }  
      17.             return cd;  
      18.         }  
    1. The main function is shown below.
        1. static void Main(string[] args)  
        2.         {  
        3.             string isNegative = "";  
        4.             try  
        5.             {  
        6.                 Console.WriteLine("Enter a Number to convert to currency");  
        7.                 string number = Console.ReadLine();  
        8.                 number = Convert.ToDouble(number).ToString();  
        9.                   
        10.                 if (number.Contains("-"))  
        11.                 {  
        12.                     isNegative = "Minus ";  
        13.                     number = number.Substring(1, number.Length - 1);  
        14.                 }  
        15.                 if (number == "0")  
        16.                 {  
        17.                     Console.WriteLine("The number in currency fomat is \nZero Only");  
        18.                 }  
        19.                 else  
        20.                 {  
        21.                     Console.WriteLine("The number in currency fomat is \n{0}", isNegative + ConvertToWords(number));  
        22.                 }  
        23.                 Console.ReadKey();  
        24.             }  
        25.             catch (Exception ex)  
        26.             {  
        27.   
        28.                 Console.WriteLine(ex.Message);  
        29.             }  
        30.             
        31.         } 

      In this function, I am checking a negative symbol and if the number contains a negative symbol, I am removing that from the number and updating isNegative string to Minus, then I am attaching this to the converted word string.

      Note

      Here, I am giving Indian currency format like if the number contains a decimal, then I am attaching “Paisa” to word string, which can be changed based on your country.

      Examples








      Up Next
        Ebook Download
        View all
        Learn
        View all