"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Efficiently Convert Numbers to Words in the Lakhs/Crores System?

How Can I Efficiently Convert Numbers to Words in the Lakhs/Crores System?

Published on 2024-11-10
Browse:564

How Can I Efficiently Convert Numbers to Words in the Lakhs/Crores System?

Transforming Numbers to Words in Lakhs / Crores System: An Efficient Approach

Converting numbers to words is a common task in programming, particularly in financial or accounting applications. While many existing solutions involve complex code with multiple regular expressions and loops, this article presents a simplified approach tailored to the specific requirements of the South Asian numbering system.

This system utilizes the concepts of "lakhs" and "crores" to represent large numbers. A lakh represents 100,000, while a crore represents 10,000,000. Unlike the Western numbering system where commas are used as separators, the South Asian system uses spaces.

To achieve this transformation efficiently, the following code snippet employs a single regular expression and eliminates the need for loops:

const a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
const 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;
  let 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;
}
````

This code combines pre-defined arrays 'a' and 'b' to form various numerical representations. By utilizing a regular expression, it captures the different sections of the number (e.g., crores, lakhs, thousands, hundreds, and ones) and generates the appropriate words. Importantly, this approach is much more concise than the earlier solution presented.

To demonstrate the code's functionality, an HTML/JavaScript snippet can be used:

document.getElementById('number').onkeyup = function () {

document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);

};


Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3