JavaScript Numbers to Words
Question:
I am trying to convert numbers to English words, such as 1234 becoming "one thousand two hundred thirty four." My function works well for most numbers, but for numbers like 190000009, it converts to "one hundred ninety million" incorrectly. Can you identify the bug and explain why it occurs?
Answer:
The provided code handles most numbers correctly, but it fails to account for the presence of zeros after non-zero digits. In the case of 190000009, when converting the group of digits "0000," the triConvert function returns "dontAddBigSuffix" due to all zeros being present. As a result, the subsequent formatting skips adding "million" to that group.
Solution:
To fix this, the code should check if the last digit of the current group is non-zero before marking the group as "dontAddBigSuffix." The modified code below corrects this issue:
... if (num == 0) { return 'dontAddBigSufix'; } else if (numString.charAt(numString.length - 1) == 0) { return 'dontAddBigSuffix'; } ...
Full Code with Modification:
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 > 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 < 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 >= 0; b--) { if (finlOutPut[b] != "dontAddBigSufix") { finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , '; bigScalCntr++; } else if (finlOutPut[b] == "dontAddBigSuffix" && finlOutPut[b-1] != "dontAddBigSuffix") { //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]; } 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'; } else if (numString.charAt(numString.length - 1) == 0) { return 'dontAddBigSuffix'; } //the case of 10, 11, 12 ,13, .... 19 if (num < 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; }
The above is the detailed content of Why does the code incorrectly convert the number '190000009' to 'one hundred ninety million' instead of 'one hundred ninety million nine'?. For more information, please follow other related articles on the PHP Chinese website!