Number to words (crore and lakh)
Number to words (Indian style - crore, lakh). The function is recursive.
const HUNDRED = 100;
const THOUSAND = HUNDRED * 10;
const LAKH = THOUSAND * 100;
const CRORE = LAKH * 100;
function number2words(num)
{
let str = '';
if (num >= CRORE)
{
str += number2words(Math.floor(num / CRORE)) + " crore";
num %= CRORE;
}
if (num >= LAKH)
{
str += number2words(Math.floor(num / LAKH)) + " lakh";
num %= LAKH;
}
if (num >= THOUSAND)
{
str += number2words(Math.floor(num / THOUSAND)) + " thousand";
num %= THOUSAND;
}
if (num >= HUNDRED)
{
str += number2words(Math.floor(num / HUNDRED)) + " hundred";
num %= HUNDRED;
}
if (num >= 10)
{
let ty = ''; // Number words ending in ty like thirty and seventy
let tn = ''; // Number words ending in the teens
const tens_digit = Math.floor(num / 10);
switch(tens_digit)
{
case 1:
switch (num)
{
case 10: tn = " ten"; break;
case 11: tn = " eleven"; break;
case 12: tn = " twelve"; break;
case 13: tn = " thirteen"; break;
case 14: tn = " fourteen"; break;
case 15: tn = " fifteen"; break;
case 16: tn = " sixteen"; break;
case 17: tn = " seventeen"; break;
case 18: tn = " eighteen"; break;
case 19: tn = " nineteen"; break;
}
break;
case 2: ty = " twenty"; break;
case 3: ty = " thirty"; break;
case 4: ty = " forty"; break;
case 5: ty = " fifty"; break;
case 6: ty = " sixty"; break;
case 7: ty = " seventy"; break;
case 8: ty = " eighty"; break;
case 9: ty = " ninety"; break;
}
if (tens_digit === 1)
{
str += tn;
}
else
{
str += ty;
num %= 10;
}
}
if (num >= 1)
{
num = Math.floor(num / 1);
switch (num)
{
case 1 : str += " one"; break;
case 2 : str += " two"; break;
case 3 : str += " three"; break;
case 4 : str += " four"; break;
case 5 : str += " five"; break;
case 6 : str += " six"; break;
case 7 : str += " seven"; break;
case 8 : str += " eight"; break;
case 9 : str += " nine"; break;
}
}
return str;
}
console.log(number2words(987654).trim());
refactored to using arrays
const HUNDRED = 100;
const THOUSAND = HUNDRED * 10;
const LAKH = THOUSAND * 100;
const CRORE = LAKH * 100;
const TN = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
const TY = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'sixteen', 'eighty', 'ninety'];
const O = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
function number2words(num)
{
let str = '';
if (num >= CRORE)
{
str += number2words(Math.floor(num / CRORE)) + " crore";
num %= CRORE;
}
if (num >= LAKH)
{
str += number2words(Math.floor(num / LAKH)) + " lakh";
num %= LAKH;
}
if (num >= THOUSAND)
{
str += number2words(Math.floor(num / THOUSAND)) + " thousand";
num %= THOUSAND;
}
if (num >= HUNDRED)
{
str += number2words(Math.floor(num / HUNDRED)) + " hundred";
num %= HUNDRED;
}
if (num >= 10)
{
const tens_digit = Math.floor(num / 10);
if (tens_digit === 1)
{
str += ' ' + TN[num - 10];
}
else
{
str += ' ' + TY[tens_digit - 2];
num %= 10;
}
}
if (num >= 1)
{
num = Math.floor(num / 1);
str += ' ' + O[num - 1];
}
return str;
}
console.log(number2words(987654).trim());
node number2words.js
nine lakh eighty seven thousand six hundred fifty four