# When i subtracting numbers and getting long decimal

In the following code the result of (change = change - 0.01) is a really long decimal does anyone know why? why arent the third and forth result 0.02 & 0.01?

console log:
0.04
0.03
0.019999999999999997
0.009999999999999997
[ [ ‘PENNY’, ‘0.04’ ] ]

``````

function checkCashRegister(price, cash, cid) {
let change = 0.05  /*change later*/

let changeArr = []
let count = 0

count = 0
for (let i=0; i<100; i++){

if (change < 0.01 || cid[0][1] < 0.01){
break;
}
if (change >= 0.01) {
count = count + 0.01;
change = change - 0.01;
cid[0][1] = cid[0][1] - 0.01;
console.log(change)

}
}

if (count > 0) {changeArr.push(["PENNY", count.toFixed(2)])}

console.log(changeArr)

}

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

Click on the magnifying glass in upper right-hand corner and search for “cash register decimal”. You will see this question has been asked numerous times before and there are really great existing forum threads you can read about this issue you are having and how to resolve it.

ok thanks i will check it out

Yes, this is a classic problem. Computers aren’t good with decimals (floats) for technical reasons that I’m not going to pretend to understand fully.

I think the way I dealt with this when I made the project is by adding a ‘fudge factor’ and rounding up (maybe I used the .toFixed() method - I don’t really remember) … but that’s not the best way to do it.

Ultimately there are multiple ways of dealing with this. How I’d do it now: I’d only use integers (like \$1.00 would just be 100, \$123.43 would be 12343, etc.) and then I’d add the decimal back for the output. But you can search around and find your own solution that makes sense to you.

Yeah i got it to work but keeping track of cents so that i wasnt dealing with decimals at all would have been a more accurate solution ill probably do that in the future. I wasnt aware that computers had problems with decimal numbers i am still pretty new to programming. Thanks for the help.