# Cash Register ----Question about the value

Cash Register ----Question about the value
0

#1

Tell us what’s happening:
The value of *nchange = nchange-(n-1)value[i] seems like to be ‘36.74’ after the first subtraction but it returns 36.739999999999995 .

``````
function checkCashRegister(price, cash, cid) {
var change=[];
var value = [0.01,0.05,0.1,0.25,1,5,10,20,100];
var  nchange= cash - price;   //The left amount of change
console.log(nchange);
for(let i=value.length-1;i>=0;i--){

for(var n=1;nchange -value[i]*n>=0;n++){  // n-1 = The number of Currency Unit
if(value[i]*n>cid[i][1]){
break;
}
}

if(n-1>0){
console.log(nchange);
console.log((n-1)*value[i]);
nchange = nchange-(n-1)*value[i];
console.log(nchange);
change.unshift([cid[i][0],(n-1)*value[i]]);
}

}
console.log(nchange);
console.log(change);
return change;
}

//checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36`.

#2

You’ve discovered one of the annoying things about javascript, the lack of floating point precision.

I had to round to the nearest cent every calculation to make sure that it did what I wanted it to do.

``````function strip(x) {
return Number.parseFloat(x).toFixed(2);
}
``````