Wrong result of simple operation( Cash Register)

Tell us what’s happening:

I’m doing the cash register stuff, and while doing that i did the following operation ;
2.05 / 0.05 . The answer is 41.

The result shown while using console.log to check out i’m doing okay was : 40.99999999

I intend to use this result in iterative operations in my following code. I would do a loop with a ( 2.05/0.05) -1 for each iteration.

Here is my question ; if i did my (2.05/0.05) -1 for each iteration, would i have done 41 or 40 with 0.04999999 left ?

If it’s just a display problem i don’t really care, however if it does only 40 iterations it would be really problematic…

I’m really far from ending my code and i don’t wish to go check if i would have had problem due to this right now. I just used Math.round in every operation to feel safe.

Thank you for your time , wish you the best.

  **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

Decimal values are stored as floating point which is, long story short, imprecise. The easiest way to prevent this problem from happening is to convert the input values into integers, do the necessary calculations, and then convert the results back to decimals for the output.

1 Like

You haven’t responded so I’ll give a more concrete hint. Even though your function receives amounts in dollars, and needs to output amounts in dollars, you could do all the necessary calculations in terms of cents.

So if the input is 1.50 dollars, convert that (and all other dollar amounts) to cents by multiplying by 100, in this case you get 150, and then you won’t have to deal with floating point rounding errors.

1 Like