2

I'm struggling to resolve a Floating Point Number issue where var change returns as 0.0999 recurring, and i need to return 0.01 (one penny). The code works fine, except the very last penny, because of this issue. This is my first post here so please excuse formatting...

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();
    } 
}

Calling

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]]);

should return

[["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]]

but it returns

[["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.03]]

Thanks in advance!

spaceman
  • 1,147
  • 8
  • 15
  • 2
    What's your actual question? If you're wondering _why_ it does this, then your answer is here: [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) If you're wondering how to fix it, then the very short answer is that it's rarely a good idea to mix floating point with monetary values. – James Thorpe May 03 '16 at 12:33
  • Yeah. I would multiply the incoming values by 100 and convert them to ints. You could also improve the performance of your algo there by using division and modulo I think... But that is a different question... – karina May 03 '16 at 12:40
  • resolved using this method, thanks! @karina – Andy Wallace May 05 '16 at 14:58

1 Answers1

0

You could get the expected answer ["PENNY", 0.04] by making the following modification for 'change' in the 'else if' condition which deals with PENNY

change = change.toFixed(2) - 0.01;