Converting Numerical Values to Indian Digit System in JavaScript

Tarun Jain
2 min readJul 20, 2023

--

Introduction

Say goodbye to the limitations of standard JavaScript toLocaleString when it comes to Indian Rupees (INR) currency representation! Introducing the game-changing convertToIndianNumberSystem polyfill, your gateway to effortlessly format numerical values in the captivating Indian digit system. Embrace the convenience of grouping units like thousands, lakhs, and crores to create an eye-catching currency display.

Understanding the Function

The provided JavaScript function, convertToIndianNumberSystem, accepts a numeric value as input and returns its corresponding string representation in the Indian digit system. Let's go through the code to grasp its working principle.

const convertToIndianNumberSystem = (num = 0) => {
// Step 1: Convert the number to a string and extract the decimal part (if present)
const inputStr = num.toString();
const [numStr, decimal] = inputStr.split(".");

// Step 2: Handle the decimal part, keeping only two decimal places
const formattedDecimal = decimal ? `.${decimal.substring(0, 2)}` : "";

// Step 3: Define regular expressions for matching number patterns in the Indian digit system
const croreRegex = /^(\d+)(\d{2})(\d{2})(\d{3})$/;
const lakhRegex = /^(\d{1,2})(\d{2})(\d{3})$/;
const thousandRegex = /^(\d{1,2})(\d{3})$/;

let match;

// Step 4: Try matching the number with the crore pattern first
if (croreRegex.test(numStr)) {
match = numStr.match(croreRegex);
match.shift(); // Remove the first element (entire matched string)
return `${match.join(",")}${formattedDecimal} Crores`;
}

// Step 5: If not matched with the crore pattern, try matching with the lakh pattern
if (lakhRegex.test(numStr)) {
match = numStr.match(lakhRegex);
match.shift(); // Remove the first element (entire matched string)
return `${match.join(",")}${formattedDecimal} Lakhs`;
}

// Step 6: If not matched with the lakh pattern, try matching with the thousand pattern
if (thousandRegex.test(numStr)) {
match = numStr.match(thousandRegex);
match.shift(); // Remove the first element (entire matched string)
return `${match.join(",")}${formattedDecimal} Thousands`;
}

// Step 7: If no pattern matches, return the original number with decimal (if present)
return `${numStr}${formattedDecimal}`;
};

Usage and Examples

console.log(convertToIndianNumberSystem(12345678)); // Output: 1,23,45,678 Crores
console.log(convertToIndianNumberSystem(9876543)); // Output: 98,76,543 Lakhs
console.log(convertToIndianNumberSystem(12345.67)); // Output: 12,345.67 Thousands
console.log(convertToIndianNumberSystem(1000)); // Output: 1,000 Thousands
console.log(convertToIndianNumberSystem(123.456)); // Output: 123.45
console.log(convertToIndianNumberSystem()); // Output: 0

Conclusion

In this article, we successfully written the JavaScript function to convert numerical values to the Indian digit system. The function accurately converts numerical values into their respective representation in thousands, lakhs, and crores. You can easily integrate this function into your JavaScript projects to handle numbers in the Indian number system efficiently.

--

--