浮点数问题JavaScript

时间:2016-05-03 12:17:55

标签: javascript floating-point precision

我正努力解决浮点数问题,其中var变化返回0.0999重复出现,我需要返回0.01(一分钱)。由于这个问题,代码工作正常,除了最后一分钱。 这是我在这里发表的第一篇文章,请原谅格式化......

function checkCashRegister(price, cash, cid) {
    var change = cash - price;     
    var totalCid =0;

    for(var i = 0; i < cid.length; i++) totalCid += cid[i][1];      

    var returnArr = [["PENNY",0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0],["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

    function hasValue (value) {return value[1] > 0;}

    if(change>totalCid) {
        return "Insufficient Funds";
    }
    else if(change==totalCid) {
        return "Closed";
    }

    else {
        while(change >= 0.01) {

            if (change >=100 && cid[8][1] >= 100){
                change -= 100;
                cid[8][1] -= 100;
                returnArr[8][1] += 100;
            }
            else if (change >= 20 && cid[7][1] >= 20){
                change -= 20;
                cid[7][1] -= 20;
                returnArr[7][1] += 20;
            }
            else if (change >= 10 && cid[6][1] >= 10){
                change -= 10;
                cid[6][1] -= 10;
                returnArr[6][1] += 10;
            }
            else if (change >= 5 && cid[5][1] >= 5){
                change -= 5;
                cid[5][1] -= 5;
                returnArr[5][1] += 5;
            }
            else if (change >= 1 && cid[4][1] >= 1){
                change -= 1;
                cid[4][1] -= 1;
                returnArr[4][1] += 1;
            }
            else if (change >= 0.25 && cid[3][1] >= 0.25) {   
                change -= 0.25;
                cid[3][1] -= 0.25;
                returnArr[3][1] += 0.25;
            }
            else if (change >= 0.10 && cid[2][1] >= 0.10){
                change -= 0.10;
                cid[2][1] -= 0.10;
                returnArr[2][1] += 0.10;
            }
            else if (change >= 0.05 && cid[1][1] >= 0.05){
                change -= 0.05;
                cid[1][1] -= 0.05;
                returnArr[1][1] += 0.05;
            }
            else if (change >= 0.01 && cid[0][1] >= 0.01){
                change -= 0.01;
                cid[0][1] -= 0.01;
                returnArr[0][1] += 0.01;
            }  
            else return "Insufficient Funds";

        }
        return returnArr.filter(hasValue).reverse();
    } 
}

调用

checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

应该返回

[[&#34; TWENTY&#34;,60.00],[&#34; TEN&#34;,20.00],[&#34; FIVE&#34;,15.00],[&#34; ONE&# 34;,1.00],[&#34; QUARTER&#34;,0.50],[&#34; DIME&#34;,0.20],[&#34; PENNY&#34;,0.0 4 ]]

但它返回

[[&#34; TWENTY&#34;,60.00],[&#34; TEN&#34;,20.00],[&#34; FIVE&#34;,15.00],[&#34; ONE&# 34;,1.00],[&#34; QUARTER&#34;,0.50],[&#34; DIME&#34;,0.20],[&#34; PENNY&#34;,0.0 3 ]]

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以通过进行以下修改来获得预期答案[&#34; PENNY&#34;,0.04]&#39;更改&#39;在&#39;否则如果&#39;处理PENNY的条件

change = change.toFixed(2) - 0.01;