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/188.8.131.52 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.
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.
I just realized. Been working without ever looking at the forum.
I’m so sorry for going missing like that. I was sure i answered you ( did not i guess). That was very helpful and was the solution for me at that time.
Thank you very much for taking the time to help.
I ended up doing it with your solution and that was perfectly fine, although a little repetitve.
As i got a bit obsessed with that ( last challenge of the certification got me pumped up) found on stackoverflow another way using:
/ const fixFloatingPoint = val => Number.parseFloat(val.toFixed(8)) /
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.