Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript numbers to Words
    primarykey
    data
    text
    <p>I'm trying to convert numbers into english words, for example <strong>1234</strong> would become: "<strong>one thousand two hundred thirty four</strong>".</p> <p>My Tactic goes like this:</p> <ul> <li><p>Separate the digits to three and put them on Array (<code>finlOutPut</code>), from right to left.</p></li> <li><p>Convert each group (each cell in the <code>finlOutPut</code> array) of three digits to a word (this what the <code>triConvert</code> function does). If all the three digits are zero convert them to <code>"dontAddBigSuffix"</code></p></li> <li><p>From Right to left, add <em>thousand, million, billion, etc</em>. If the <code>finlOutPut</code> cell equals <code>"dontAddBigSufix"</code> (because it was only zeroes), don't add the word and set the cell to <code>" "</code> (nothing).</p></li> </ul> <p>It seems to work pretty well, but I've got some problems with numbers like 19000000<strong>9</strong>, converted to: "<em>one hundred ninety million</em>". Somehow it "forgets" the last numbers when there are a few zeros.</p> <p>What did I do wrong? Where is the bug? Why does it not work perfectly?</p> <pre> &#60;html&#62; &#60;head&#62; &#60;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&#62; &#60;script type="text/javascript"&#62; function update(){ var bigNumArry = new Array('', ' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion'); var output = ''; var numString = document.getElementById('number').value; var finlOutPut = new Array(); if (numString == '0') { document.getElementById('container').innerHTML = 'Zero'; return; } if (numString == 0) { document.getElementById('container').innerHTML = 'messeg tell to enter numbers'; return; } var i = numString.length; i = i - 1; //cut the number to grups of three digits and add them to the Arry while (numString.length &#62; 3) { var triDig = new Array(3); triDig[2] = numString.charAt(numString.length - 1); triDig[1] = numString.charAt(numString.length - 2); triDig[0] = numString.charAt(numString.length - 3); var varToAdd = triDig[0] + triDig[1] + triDig[2]; finlOutPut.push(varToAdd); i--; numString = numString.substring(0, numString.length - 3); } finlOutPut.push(numString); finlOutPut.reverse(); //conver each grup of three digits to english word //if all digits are zero the triConvert //function return the string "dontAddBigSufix" for (j = 0; j &#60; finlOutPut.length; j++) { finlOutPut[j] = triConvert(parseInt(finlOutPut[j])); } var bigScalCntr = 0; //this int mark the million billion trillion... Arry for (b = finlOutPut.length - 1; b &#62;= 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&#60;finlOutPut.length; n++){ output +=finlOutPut[n]; } document.getElementById('container').innerHTML = output;//print the output } //simple function to convert from numbers to words from 1 to 999 function triConvert(num){ var ones = new Array('', ' one', ' two', ' three', ' four', ' five', ' six', ' seven', ' eight', ' nine', ' ten', ' eleven', ' twelve', ' thirteen', ' fourteen', ' fifteen', ' sixteen', ' seventeen', ' eighteen', ' nineteen'); var tens = new Array('', '', ' twenty', ' thirty', ' forty', ' fifty', ' sixty', ' seventy', ' eighty', ' ninety'); var hundred = ' hundred'; var output = ''; var numString = num.toString(); if (num == 0) { return 'dontAddBigSufix'; } //the case of 10, 11, 12 ,13, .... 19 if (num &#60; 20) { output = ones[num]; return output; } //100 and more if (numString.length == 3) { output = ones[parseInt(numString.charAt(0))] + hundred; output += tens[parseInt(numString.charAt(1))]; output += ones[parseInt(numString.charAt(2))]; return output; } output += tens[parseInt(numString.charAt(0))]; output += ones[parseInt(numString.charAt(1))]; return output; } &#60;/script&#62; &#60;/head&#62; &#60;body&#62; &#60;input type="text" id="number" size="70" onkeyup="update();" /*this code prevent non numeric letters*/ onkeydown="return (event.ctrlKey || event.altKey || (47&#60;event.keyCode && event.keyCode&#60;58 && event.shiftKey==false) || (95&#60;event.keyCode && event.keyCode&#60;106) || (event.keyCode==8) || (event.keyCode==9) || (event.keyCode&#62;34 && event.keyCode&#60;40) || (event.keyCode==46) )"/&#62; &#60;br/&#62; &#60;div id="container"&#62;Here The Numbers Printed&#60;/div&#62; &#60;/body&#62; &#60;/html&#62; </pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload