Convert A Number Into Words Using Recursion

Introduction

This article explains how to convert a number into words using recursion. I am posting this article to help the developers in the situation where they need to convert an amount into words for reporting purposes and making data more informative. This article takes an amount or a number as input and converts it into words, as shown below.

Convert Number To Words Using Recursion 
 
Background

Many times, while generating reports or logging/processing the data, there are situations when we need to show the numbers as words. This is a challenging task as no one would like to spend much time on writing code for this. In here, we will use recursion for the conversion.

Recursion

When a method calls itself within the method body, the process is called recursion. The method which is calling itself is called a recursive method or recursive function. To convert a number into words, we can use recursion in a managed way.
 
Code Implementation 

Here, I am writing a method which takes a number as input and returns the converted number as a string, as shown above.

This method is applicable to the numbers from 0 to 999999999. This method can be extended for a larger number as well by adding some if conditions and adding the values in unitsMap and tensMap array.

  1. private static string ConvertIntoWords(long number)  
  2.         {  
  3.             if (number == 0)  
  4.                 return "zero";  
  5.             if (number < 0)  
  6.                 return "minus " + ConvertIntoWords(Math.Abs(number));  
  7.             string words = "";  
  8.             if ((number / 10000000) > 0)  
  9.             {  
  10.                 words += ConvertIntoWords(number / 10000000) + " crores ";  
  11.                 number %= 10000000;  
  12.             }  
  13.             if ((number / 100000) > 0)  
  14.             {  
  15.                 words += ConvertIntoWords(number / 100000) + " lacs ";  
  16.                 number %= 100000;  
  17.             }  
  18.             if ((number / 1000) > 0)  
  19.             {  
  20.                 words += ConvertIntoWords(number / 1000) + " thousand ";  
  21.                 number %= 1000;  
  22.             }  
  23.             if ((number / 100) > 0)  
  24.             {  
  25.                 words += ConvertIntoWords(number / 100) + " hundred ";  
  26.                 number %= 100;  
  27.             }  
  28.             if (number > 0)  
  29.             {  
  30.                 var unitsMap = new[] { "zero""one""two""three""four""five""six""seven""eight""nine""ten""eleven""twelve""thirteen""fourteen""fifteen""sixteen""seventeen""eighteen""nineteen" };  
  31.                 var tensMap = new[] { "zero""ten""twenty""thirty""forty""fifty""sixty""seventy""eighty""ninety" };  
  32.                 if (number < 20)  
  33.                     words += unitsMap[number];  
  34.                 else  
  35.                 {  
  36.                     words += tensMap[number / 10];  
  37.                     if ((number % 10) > 0)  
  38.                         words += " " + unitsMap[number % 10];  
  39.                 }  
  40.             }  
  41.             return words;  
  42.         }  

The above code is self explanatory. Here, we have an input parameter which takes a number as input.

  1. If the number is 0, the method returns 0.
  2. If the number is negative, it returns minus and calls the method again with an absolute value, i.e., a non-negative value.
  3. Then, it checks the conditions one by one and adds the remainder in the word format into the converted string. Then, the function calls itself again and again with the quotient value.
  4. When a number is between 0 and 9 or 10, 20, 30, 40, 50, 60, 70, 80 and 90, then it maps the value from units and tens map and appends to the word.
  5. Finally, the converted number in the fourth step is returned by the method.

Below is the console application's main method which calls the above method.

  1. static void Main(string[] args)  
  2.         {  
  3.             Console.WriteLine("Enter a number");  
  4.             int number = Convert.ToInt32(Console.ReadLine());  
  5.             Console.WriteLine("Word format is : " +ConvertIntoWords(number));  
  6.             Console.ReadKey();  
  7.         }  
Output
 
Below is the output of the program. We have a console asking to input a number. As soon as we press Enter after typing in the number, the converted word is printed in the console.
 
 
 
Point of interest
 
The above code version is not bound to the console application only. Any C# application can call the conversion method and get output in the word format. This is very much required when we prepare reports from data because it is never recommended to store a number in word format in the database or memory. 


Similar Articles