convert number to words javascript lakh

Convert Number to Words in JavaScript for Lakh

Converting a number to words in JavaScript can be a useful feature for various applications. While there are many different ways to accomplish this, we will focus on converting numbers into words for the lakh unit, which is commonly used in South Asian countries like India, Pakistan, and Bangladesh.

Method 1: Using JavaScript functions

One way to convert a number to words is by using a series of JavaScript functions. Here is an example code snippet:


function convertNumberToWords(num) {
  var words = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"];
  var tens = ["Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
  var hundreds = ["Hundred", "Thousand", "Lakh"];

  if (num == 0) {
    return words[0];
  }

  var wordsString = "";

  // Convert tens and ones place
  if (num % 100 < 20) {
    wordsString = words[num % 100];
    num = Math.floor(num / 100);
  } else {
    wordsString = words[num % 10];
    num = Math.floor(num / 10);

    wordsString = tens[num % 10 - 1] + " " + wordsString;
    num = Math.floor(num / 10);
  }

  // Convert hundreds place
  if (num % 10 !== 0) {
    wordsString = words[num % 10] + " " + hundreds[0] + " " + wordsString;
    num = Math.floor(num / 10);
  }

  // Convert thousands place
  if (num % 100 !== 0) {
    wordsString = convertNumberToWords(num % 100) + " " + hundreds[1] + " " + wordsString;
    num = Math.floor(num / 100);
  }

  // Convert lakh place
  if (num !== 0) {
    wordsString = convertNumberToWords(num) + " " + hundreds[2] + " " + wordsString;
  }

  return wordsString;
}

console.log(convertNumberToWords(123456));

In this example, we define three arrays: one for words from zero to nine, one for words from ten to ninety, and one for the place values (hundred, thousand, lakh). We then check each place value starting from right to left and convert it to words.

Method 2: Using External Libraries

Another way to convert a number to words is by using external libraries like number-to-words. Here is an example code snippet:


const numberToWords = require('number-to-words');

console.log(numberToWords.toWords(123456));

In this example, we import the number-to-words library and use its toWords() function to convert the number to words. This library supports multiple languages and units including lakh.

Method 3: Using Regular Expressions

Finally, we can also use regular expressions to replace numbers with their corresponding word values. Here is an example code snippet:


const number = 123456;
const words = number.toString().replace(/\d+/g, function (match) {
  return match.split('').map(function (digit) {
    return ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'][digit];
  }).join('');
});

console.log(words);

In this example, we convert the number to a string and use a regular expression to replace each digit with its corresponding word value. We then join the resulting array of words to create the final string.

Conclusion

Converting a number to words in JavaScript for lakh can be accomplished in several different ways. Whether you choose to use JavaScript functions, external libraries, or regular expressions, each method has its own advantages and disadvantages. With the right approach, you can quickly and easily convert numbers to words for your specific use case.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe