How to Convert Numbers to Words in the Indian Numbering System Using JavaScript
In many business applications, it is often necessary to display numbers in words, especially when dealing with currency. In the Indian numbering system, amounts are represented in units like "thousand," "lakh," and "crore," and this format is different from the international system.
This article explains how to convert a number into words up to 40 crores using a JavaScript function.
Requirements
We will create a JavaScript function that converts a number into words in the Indian currency format. The system should handle:
Whole numbers like thousands, lakhs, and crores.
Fractional numbers representing paisa.
Input validation to handle invalid inputs like non-numeric values.
The Code
Here's the JavaScript code that will convert any given number up to 40 crores into words, including fractional paisa values.
How the Code Works
1. Input Validation
The convertNumberToWords()
function begins by checking whether the input is valid. It strips out commas or spaces and checks if the number is valid using isNaN()
. If the number is invalid, the function returns "invalid input."
2. Splitting the Number
The function then splits the number into two parts:
Whole part: The portion before the decimal point.
Fractional part: The portion after the decimal point (if any).
The whole part is converted into words using the convertWholePartToWords()
function, and the fractional part is processed by the convertFractionPartToWords()
function to get the paisa in words.
3. Converting the Whole Part
The convertWholePartToWords()
function takes care of breaking the number into chunks and converting each chunk into words. The chunks represent:
Hundreds (e.g., 100, 200, 300, etc.),
Thousands (e.g., 1000, 2000, etc.),
Lakhs (e.g., 100000, 200000, etc.),
Crores (e.g., 10000000, 20000000, etc.).
Each chunk is passed to the getWordsForChunk()
function, which converts the digits into words.
4. Converting the Fractional Part (Paisa)
The convertFractionPartToWords()
function converts the fractional part (paisa) into words, using the same logic as for the whole part, but only for the paisa portion.
5. Formatting the Result
Once both the whole and fractional parts are converted into words, the function formats the output as "X rupees and Y paisa" (or "only" if there is no paisa).
Example Usage
Let’s look at how this code works with a few examples:
Example 1: Convert 15,00,000 into words
Output:
Example 2: Convert 4,50,75,212 into words
Output:
Example 3: Convert 2,34,567.89 into words
Output:
Conclusion
This JavaScript function allows you to easily convert numbers into words in the Indian currency system, which is commonly used in India for business and financial applications. It handles both large values up to 40 crores and fractional amounts for paisa.
By using this code, you can easily integrate number-to-word conversion into your applications for invoicing, financial statements, or any other use cases requiring the representation of numbers in word format.
Last updated