Free javascript Hosting


convertnumtoword.js

Uploaded on Apr 24 2022 16:36 by abhij.v.g

// function to convert number to word

//recursive function which converts given number to word
function covertNumToWord(value) {
const inputValueLength = value.length
const inputValueArray = Array.from(value)
const isInValid = inputValueArray.every(item => item === "0");
if (isInValid){
return ""
}
if (inputValueLength < 4 ) {
return getArray(inputValueArray)
}else {
if (inputValueLength%2 === 0) {
return getWord(inputValueArray[0])+ " "+getWord(Math.pow(10,inputValueLength-1).toString()) + " "+
covertNumToWord(inputValueArray.slice(1,inputValueLength))
}else{
const val = parseInt(inputValueArray[0]+inputValueArray[1])
if (val === 0) {
return "" + covertNumToWord(inputValueArray.slice(2,inputValueLength))

} else if (val < 20) {
return getWord(val.toString()) + " " +
getWord(Math.pow(10,inputValueLength-2).toString()) + " " +
covertNumToWord(inputValueArray.slice(2,inputValueLength))
} else{
return getWord(inputValueArray[0]+"0") + getWord(inputValueArray[1]) +" " +
getWord(Math.pow(10,inputValueLength-2).toString()) + " " +
covertNumToWord(inputValueArray.slice(2,inputValueLength))
}

}
}
}

// function to handle ones,tens,hundreds,thousands
function getArray(inputValueArray){
const inputValueLength = inputValueArray.length
switch (inputValueLength) {
case 1:
return getWord(inputValueArray[0])
case 2:
const val = parseInt(inputValueArray[0]+inputValueArray[1])
return val === 0 ? "" : val < 20 ?
getWord(val.toString()) :
getWord((parseInt(inputValueArray[0]) * 10).toString()) + " "+getWord(inputValueArray[1])
case 3:
const firstDigitData = inputValueArray[0] === "0" ? "" :getWord(inputValueArray[0]) + " " +
getWord(Math.pow(10,inputValueLength-1).toString())
const remDegits = parseInt(inputValueArray[1]+inputValueArray[2]) < 20 ?
getWord(parseInt(inputValueArray[1] + inputValueArray[2]).toString()) :
getWord((parseInt(inputValueArray[1]) * 10).toString()) + " "+getWord(inputValueArray[2])
return firstDigitData + " "+remDegits
}
}

// metadata for the required numbers
function getWord(number){
switch (number) {
case "0":
return ""
case "1":
return "One"
case "2":
return "Two"
case "3":
return "three"
case "4":
return "four"
case "5":
return "five"
case "6":
return "six"
case "7":
return "seven"
case "8":
return "eight"
case "9":
return "nine"
case "10":
return "ten"
case "11":
return "eleven"
case "12":
return "twelve"
case "13":
return "thirteen"
case "14":
return "fourteen"
case "15":
return "fifteen"
case "16":
return "sixteen"
case "17":
return "seventeen"
case "18":
return "eighteen"
case "19":
return "nineteen"
case "20":
return "twenty"
case "30":
return "thirty"
case "40":
return "fourty"
case "50":
return "fifty"
case "60":
return "sixty"
case "70":
return "seventy"
case "80":
return "eighty"
case "90":
return "ninety"
case "100":
return "hundred"
case "1000":
return "thousand"
case "100000":
return "lakh"
case "10000000":
return "crore"

default:
break;
}
}

console.log(covertNumToWord("001"))

Back to list