首页  >  文章  >  web前端  >  为什么代码错误地将数字'190000009”转换为'一亿九千万”而不是'一亿九千万九”?

为什么代码错误地将数字'190000009”转换为'一亿九千万”而不是'一亿九千万九”?

Barbara Streisand
Barbara Streisand原创
2024-11-21 02:01:13726浏览

Why does the code incorrectly convert the number

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn