Created
October 20, 2020 00:58
-
-
Save anjanesh/0d77f7749fe4b0dedd41662493dc6afe to your computer and use it in GitHub Desktop.
Number to words (crore and lakh)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment