335.40999999999997 instead of 335.41

Tell us what’s happening:

Right now I’m just trying to get a total amount of money of the “cid” variable and i keep getting 335.40999999999997 instead of 335.41. I’ve read a few forum posts about similar problems and have tried applying the same solutions but I am still getting the same results. I think i might be applying it incorrectly but I am unsure . I would really appreciate and advice regarding this problem .

Your code so far


function checkCashRegister(price, cash, cid) {
let cid_total = 0; 
for (let i in cid) { 
  cid_total = Number.parseFloat(cid_total + cid[i][1])
} 
console.log(cid_total);
var 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]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

I recommend using an integer number of cents instead of a decimal number of dollars.

tried this and still got the same result . thanks tho

So do you mean to have two separate integers. one for cents and one for dollars ?

Math.Round may work.

no, you convert everything to a whole number of cents

so instead of doing for example 0.1 + 0.2, you first convert them to a whole number of cents multiplying by 100, so you have 10 + 20, result 30 - when you wabt to output the result you divide by 100: result is 0.3

whole numbers do not have floating point issues

1 Like

Yea that makes sense! thank you ! I was hoping there would be a more straight forward approach, but this is great too .