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?
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.