I made a code for Cash Register challenge but it doesn’t work for one of tests. After debugging I realized it’s due to the popular problem of computing floating point numbers. Is there any way to guarantee the outcome. I tried to use `.toFixed()`

and the algorithm works for the mentioned test but spoils the outcome for another test;

Personally, I prefer to convert the entire cash register to an integer number of cents.

Thank you that’s clever. So I need to divide the change value by `0.01`

. But to make it sure, `0.01`

is floating point too, is there any possibility for division of a number by `0.01`

to give wrong result? Especially because I read this problem is due to computers’ inability to store `0.1`

.

And do you know any workaround to guarantee the result of calculation?

I *multiply* by 100, do the calculations on the drawer, then *divide* by 100. You will need to round after multiplying to get an integer though.

Yea, both the change and the drawer need to be converted into cents. You don’t do the division until the very end.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.