Skip to content
the poor coder

convert number to words javascript lakh

Poor Man
Published
Reading time
3 min read
On this page

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:

Text

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:

Text

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:

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC