Last active
January 27, 2023 09:29
-
-
Save poonamdhomane/9bcbfd2816e3d24c5caa0052a07cfadc to your computer and use it in GitHub Desktop.
Convert numbers to words using JavaScript
This file contains hidden or 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
<html> | |
<head> | |
<title>HTML - Convert numbers to words using JavaScript</title> | |
<script type="text/javascript"> | |
function onlyNumbers(evt) { | |
var e = event || evt; // For trans-browser compatibility | |
var charCode = e.which || e.keyCode; | |
if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; | |
return true; | |
} | |
function convert(x, zeroTo999, place) { | |
let n = x.length; | |
x = n === 0 ? "00" : n === 1 || n % 2 === 0 ? "0" + x : x; | |
n = x.length; | |
let r = | |
zeroTo999[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528]; | |
console.log("r ===", r); | |
if (n >= 1) { | |
const v = zeroTo999[x.charCodeAt((n -= 1)) - 48]; | |
if (v) r = v + " Hundred" + (r ? " " + r : ""); | |
console.log("r ===", r); | |
console.log("v ===", v); | |
} | |
for (let i = 0; n > 0; i++) { | |
const v = | |
zeroTo999[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528]; | |
console.log("v ===", v); | |
if (v) r = v + " " + place[i] + (r ? " " + r : ""); | |
console.log("r ===", r); | |
r = r.replace("Ninety Nine Arab", "Nine Thousand Nine Hundred"); | |
r = r.replace("Nine Arab", "Nine Hundred"); | |
} | |
return r; | |
} | |
function NumToWord(inputNumber, outputControl) { | |
var str = new String(inputNumber); | |
var zeroTo999 = []; | |
var place = "Thousand|Lakh|Crore|Arab|Kharab|Nil".split("|"); | |
const ones = | |
"|One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven|Twelve|Thirteen|Fourteen|Fifteen|Sixteen|Seventeen|Eighteen|Nineteen".split( | |
"|" | |
); | |
const tens = | |
"||Twenty|Thirty|Forty|Fifty|Sixty|Seventy|Eighty|Ninety".split("|"); | |
for (let i = 0; i < 100; i++) { | |
const t = Math.floor(i / 10); | |
const o = i % 10; | |
zeroTo999.push(t < 2 ? ones[i] : tens[t] + (o ? " " + ones[o] : "")); | |
} | |
let finalOutput = convert(inputNumber, zeroTo999, place); | |
document.getElementById(outputControl).innerHTML = finalOutput; | |
} | |
</script> | |
</head> | |
<body> | |
<h1>HTML - Convert numbers to words using JavaScript</h1> | |
<input | |
id="Text1" | |
type="text" | |
onkeypress="return onlyNumbers(this.value);" | |
onkeyup="NumToWord(this.value,'divDisplayWords');" | |
maxlength="11" | |
style=" | |
background-color: #efefef; | |
border: 2px solid #CCCCC; | |
font-size: large; | |
" | |
/> | |
<br /> | |
<br /> | |
<div | |
id="divDisplayWords" | |
style="font-size: 13; color: Teal; font-family: Arial" | |
></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try running this html file directly