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.

let thousands = ['', 'thousand', 'lakh', 'crore'];
let digits = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
let tens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
let twenties = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

function convertNumberToWords(number) {
    if (number === undefined || number === null) return 'invalid input';

    number = number.toString().replace(/[\, ]/g, ''); // Remove commas and spaces.
    if (isNaN(number)) return 'invalid input'; // Check if the input is a valid number.

    let decimalIndex = number.indexOf('.');
    let fractionPart = decimalIndex !== -1 ? number.slice(decimalIndex + 1) : '';
    let wholePart = decimalIndex !== -1 ? number.slice(0, decimalIndex) : number;
    if (wholePart.length > 15) return 'too big'; // Handle numbers that are too large.

    let word = convertWholePartToWords(wholePart) + ' rupees'; // Convert the whole part to words.

    if (fractionPart) {
        let fractionWords = convertFractionPartToWords(fractionPart);
        word += ' and ' + fractionWords + ' paisa'; // Convert the fractional part to paisa.
    } else {
        word += ' only';
    }

    return word.replace(/\s+/g, ' ').trim(); // Remove extra spaces.
}

function convertWholePartToWords(num) {
    let word = '';
    let unitIndex = 0;

    // Process the number in chunks of 3 or 2 digits, depending on the unit.
    while (num.length > 0) {
        let chunkSize;
        if (unitIndex === 1 || unitIndex === 2) { // Handle thousands and lakhs
            chunkSize = 2;
        } else if (unitIndex === 3) { // Handle crores
            chunkSize = 2;
        } else { // Handle the hundreds place
            chunkSize = 3;
        }

        let chunk = num.slice(-chunkSize);
        num = num.slice(0, -chunkSize);
        
        let chunkNumber = parseInt(chunk, 10);
        if (chunkNumber > 0) {
            if (unitIndex === 0) {
                word = getWordsForChunk(chunkNumber) + ' ' + word;
            } else {
                word = getWordsForChunk(chunkNumber) + ' ' + thousands[unitIndex] + ' ' + word;
            }
        }
        unitIndex++;
    }

    return word.trim();
}

function convertFractionPartToWords(num) {
    let fractionNumber = parseInt(num, 10);
    if (fractionNumber === 0) return '';
    return getWordsForChunk(fractionNumber);
}

function getWordsForChunk(chunkNumber) {
    let chunkWord = '';
    if (chunkNumber >= 100) {
        chunkWord += digits[Math.floor(chunkNumber / 100)] + ' hundred ';
        chunkNumber = chunkNumber % 100;
    }
    if (chunkNumber >= 20) {
        chunkWord += twenties[Math.floor(chunkNumber / 10) - 2] + ' ';
        chunkNumber = chunkNumber % 10;
    }
    if (chunkNumber > 0 && chunkNumber < 10) {
        chunkWord += digits[chunkNumber] + ' ';
    } else if (chunkNumber >= 10) {
        chunkWord += tens[chunkNumber - 10] + ' ';
    }
    return chunkWord.trim();
}

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

convertNumberToWords(1500000);

Output:

"fifteen lakh rupees only"

Example 2: Convert 4,50,75,212 into words

convertNumberToWords(45075212);

Output:

"four crore fifty lakh seventy-five thousand two hundred twelve rupees only"

Example 3: Convert 2,34,567.89 into words

convertNumberToWords(234567.89);

Output:

"two lakh thirty-four thousand five hundred sixty-seven rupees and eighty-nine paisa"

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