Efficient Implementation of Number to Words Conversion in Lakh/Crore System Using JavaScript

Nov 24, 2025 · Programming · 7 views · 7.8

Keywords: JavaScript | Number Conversion | Lakh System | Crore System | Regular Expressions

Abstract: This paper provides an in-depth exploration of efficient methods for converting numbers to words in the Lakh/Crore system using JavaScript. By analyzing the limitations of traditional implementations, we propose an optimized solution based on regular expressions and string processing that supports accurate conversion of up to 9-digit numbers. The article details core algorithm logic, data structure design, boundary condition handling, and includes complete code implementation with performance comparison analysis.

Introduction

Number to words conversion plays a crucial role in financial systems, invoice generation, and multilingual applications. While traditional Western numbering systems use units like Million and Billion, South Asian numbering systems employ unique units such as Lakh (105) and Crore (107). Based on high-scoring Stack Overflow answers, this paper presents a concise and efficient JavaScript implementation.

Limitations of Traditional Implementations

The original questioner's code employed complex loop structures and regular expression processing:

var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

This implementation contained two regular expressions and two for loops, resulting in high code complexity, and only supported Western numbering systems, failing to meet the specific requirements of South Asian regions.

Core Design of Optimized Solution

We adopt a simplified approach based on string preprocessing and regular expression matching, with the core data structure designed as follows:

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

Detailed Algorithm Implementation

The implementation logic of the main function inWords includes the following key steps:

Input Validation and Preprocessing

function inWords(num) {
    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return;

First, convert the input number to a string and check if the length exceeds the 9-digit limit. Ensure uniform format through string padding, and use regular expressions to split the number into five parts: crore, lakh, thousands, hundreds, and tens/units.

Unit Processing Logic

str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';

For each digit segment, first check if it is 0. For non-zero values, determine if it falls within the 1-19 range (directly using array a) or the 20-99 range (combining arrays b and a).

Connector Word Processing

str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';

Add the "and" connector before the last two digits and append "only" at the end to indicate exact values.

Complete Code Implementation

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

function inWords(num) {
    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return; var str = '';
    str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
    str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
    str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
    str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
    str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
    return str;
}

Application Scenarios and Extensions

This implementation can be directly integrated into web applications:

document.getElementById('number').onkeyup = function () {
    document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);
};

Combining with the Lua implementation mentioned in the reference article, we can further extend support for decimal part processing. The Lua implementation handles decimal parts through the math.round function and adds unit descriptions like "Hundredths".

Performance Analysis and Comparison

Compared with traditional implementations, this solution offers the following advantages:

Discussion of Limitations

The main limitations of the current implementation include:

Conclusion

The number to words conversion implementation in the Lakh/Crore system proposed in this paper significantly simplifies the complexity of traditional implementations through clever string preprocessing and regular expression matching. This solution demonstrates good performance and maintainability in practical applications, providing an effective technical solution for number processing needs in South Asian regions. Future work could focus on supporting larger number ranges, decimal processing, and broader internationalization support.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.