Home > Web Front-end > JS Tutorial > body text

How can I convert Indian numbers to words using the lakh/crore system?

Susan Sarandon
Release: 2024-11-06 12:19:02
Original
580 people have browsed it

How can I convert Indian numbers to words using the lakh/crore system?

Transform Indian Numbers to Words Using Lakh/Crore System

When working with Indian numbers, it becomes necessary to convert them to their corresponding word forms. While there are several approaches to achieve this, the code provided might seem overly complex. Let's explore a simpler alternative:

The following function uses a single regular expression to efficiently convert numbers into their word form:

<code class="javascript">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;
}</code>
Copy after login

This code utilizes regular expressions to handle various scenarios, such as determining the number of crores, lakhs, thousands, and hundreds in the given number. The output is then constructed based on these values.

Usage and Example:

Using the code:

<code class="html"><span id="words"></span>
<input id="number" type="text"></code>
Copy after login

When you type a number into the input field and release a key, the inWords() function is executed, converting the number to its word form and displaying it in the words span.

This code provides a compact, easy-to-understand solution that addresses your need for transforming Indian numbers to words in the lakh/crore system.

The above is the detailed content of How can I convert Indian numbers to words using the lakh/crore system?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!