JavaScript 數字到單字
問題:
我正在嘗試將數字轉換為英文單字,例如1234 變成「一千二百三十四」。我的函數對於大多數數字都適用,但對於像 190000009 這樣的數字,它會錯誤地轉換為「一億九千萬」。您能找出該錯誤並解釋它發生的原因嗎?
答案:
提供的程式碼可以正確處理大多數數字,但無法解釋之後出現的零非零數字。在 190000009 的情況下,當轉換數字組“0000”時,triConvert 函數會傳回“dontAddBigSuffix”,因為所有零都存在。因此,後續格式會跳過向該組添加“million”。
解決方案:
要解決此問題,代碼應檢查該組的最後一位數字是否為在將組標記為“dontAddBigSuffix”之前,當前組不為零。以下修改後的程式碼修正了此問題:
... if (num == 0) { return 'dontAddBigSufix'; } else if (numString.charAt(numString.length - 1) == 0) { return 'dontAddBigSuffix'; } ...
經過修改的完整程式碼:
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; }
以上是為什麼代碼錯誤地將數字'190000009”轉換為'一億九千萬”而不是'一億九千萬九”?的詳細內容。更多資訊請關注PHP中文網其他相關文章!