@anjanesh/number-to-words-inr
Version:
Integer number to Indian numbering currency system featuring Crores, Lakhs, Thousands and Hundreds
64 lines (52 loc) • 1.6 kB
JavaScript
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', 'eighty', 'ninety'];
const O = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
export function number2words(num)
{
if (num == 0) return 'zero';
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];
num = 0; // This is to prevent for example : 12 to trickle down as 12%10 = 2
}
else
{
str += ' ' + TY[tens_digit - 2];
num %= 10;
}
}
if (num >= 1)
{
num = Math.floor(num / 1);
str += ' ' + O[num - 1];
}
return str;
}