JavaScript Numbers to Words

Transform numbers to words in lakh / crore system

Update: Looks like this is more useful than I thought. I've just published this on npm. https://www.npmjs.com/package/num-words


Here's a shorter code. with one RegEx and no loops. converts as you wanted, in south asian numbering system

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
function inWords (num) { if ((num = num.toString()).length > 9) return 'overflow'; n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/); if (!n) return; var str = ''; str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : ''; str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : ''; str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : ''; str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : ''; str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : ''; return str;}
document.getElementById('number').onkeyup = function () { document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);};
<span id="words"></span><input id="number" type="text" />

JavaScript numbers to Words

JavaScript is parsing the group of 3 numbers as an octal number when there's a leading zero digit. When the group of three digits is all zeros, the result is the same whether the base is octal or decimal.

But when you give JavaScript '009' (or '008'), that's an invalid octal number, so you get zero back.

If you had gone through the whole set of numbers from 190,000,001 to 190,000,010 you'd hav seen JavaScript skip '...,008' and '...,009' but emit 'eight' for '...,010'. That's the 'Eureka!' moment.

Change:

for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
}

to

for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j],10));
}

Code also kept on adding commas after every non-zero group, so I played with it and found the right spot to add the comma.

Old:

for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , ';
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}

//convert The output Arry to , more printable string
for(n = 0; n<finlOutPut.length; n++){
output +=finlOutPut[n];
}

New:

for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr]; // <<<
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}

//convert The output Arry to , more printable string
var nonzero = false; // <<<
for(n = 0; n<finlOutPut.length; n++){
if (finlOutPut[n] != ' ') { // <<<
if (nonzero) output += ' , '; // <<<
nonzero = true; // <<<
} // <<<
output +=finlOutPut[n];
}

NUMBERS TO WORDS using html and javascript

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<br><br>
<h1>NUMBER TO WORDS</h1>
<input type="5ber" id="nm1" placeholder="number"><br><br>
<input type="button" value="To Words" id="optin" onclick="pldstrng()">
<p id="value" style="color: blueviolet;"></p>

<script>
function pldstrng() {
var k = document.getElementById("nm1").value
var a = String(k)
for (let i = 0; i < a.length; i++) {
switch (a[i]) {
case "1":
document.getElementById("value").innerHTML+="one"+" "
break
case "2":
document.getElementById("value").innerHTML+="two"+" "
break
case "3":
document.getElementById("value").innerHTML+="three"+" "
break
case "4":
document.getElementById("value").innerHTML+="four"+" "
break
case "5":
document.getElementById("value").innerHTML+="five"+" "
break
case "6":
document.getElementById("value").innerHTML+="six"+" "
break
case "7":
document.getElementById("value").innerHTML+="siven"+" "
break
case "8":
document.getElementById("value").innerHTML+="eight"+" "
break
case "9":
document.getElementById("value").innerHTML+="nine"+" "
break
case "0":
document.getElementById("value").innerHTML+="zero"+" "
break
default :
document.getElementById("value").innerHTML="enter a number / digit"+" "
}
}
}
</script>
</body>

</html>

Numbers to words along with decimal value count

Use the same function for getting the whole and the decimal part

function withDecimal(n) {
var nums = n.toString().split('.')
var whole = convertNumberToWords(nums[0])
if (nums.length == 2) {
var fraction = convertNumberToWords(nums[1])
return whole + 'and ' + fraction;
} else {
return whole;
}
}

console.log(withDecimal(51.32)) //Fifty One and Thirty Two
console.log(withDecimal(29.0)) //Twenty Nine

Simple Javascript to Convert Numbers from 1 to 11 into Words?

function numeral(m){
var numbers = ['zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven'];
return numbers[m]
}

var x = numeral(3);
console.log(x)

numeralES6=(m)=>{
var numbers = ['zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven'];
return numbers[m]
}

var y = numeralES6(4);
console.log(y)

Convert numbers to words but can't convert with duplicate numbers

The replace, replaces the first occurance of the string.
You don't need the '<=' btw, '<' is enough.

function NumbersToWords(number) {
var numbersArray = [1, 2, 3, 4, 5];
var wordsArray = ["one", "two", "three", "four", "five"];
for (let i = 0; i < numbersArray.length; i++) {
number = number.toString().replaceAll(numbersArray[i], wordsArray[i]);
}
return number;
}
console.log(NumbersToWords(122));
console.log(NumbersToWords(1224));


Related Topics



Leave a reply



Submit