The decimal is getting incremented when its rounded off to the nearest hundredth :/

Tell us what’s happening:
Im on the cash register js challenge (the last one). I managed to pass everything but one -

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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

For some reason the decimal integer increases. Like for example -

Ive put a comment at the area of concern.
[Note: I know the code is messy so please ignore it :slight_smile: ]

The code is kinda big … so the fcc box didnt allow me to post the full code here, so i had to make a codepen project -

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

Challenge: Cash Register

Link to the challenge:

Floating point (decimal) numbers always have roundoff. However, integers never do. I recommend doing all of your calculations in an integer number of cents and then converting back to a decimal number of dollars before returning the output.

1 Like

Thanks !
Using this line of thought, I found a way to fix it using


That works to. I tend not to do it because it actually converts all of your numbers to strings.

1 Like

Yeah, the toFixed() converts it into a string , so I used the parseFloat() to convert it into a number(floating-point) .

Just be aware conversion to a string and then parsing leaves the low-order bits of the mantissa effectively filled with garbage. Probably not an issue here, but might be if you carry on additional calculations assuming full precision. If you really only need a couple decimal digits of precision, you’re often better off with integer or fixed-point arithmetic.

Goldbergs 1991 paper What Every Computer Scientist Should Know About Floating Point Arithmetic is a wealth of useful information.