I am trying to round a number in Javascript to two decimal places. I consulted the following Stack Overflow post for guidance:
Round to at most 2 decimal places (only if necessary)
Now, in that post, there seems to be two ways to accomplish this. One is toFixed(2)
, and the other is Math.round((num + Number.EPSILON) * 100) / 100
, with the latter seeming to be the most accepted way of doing it.
So, given the number 249.025
, this works. Both methods produce 249.03
as expected. However, given the number 294.025
(flip the 4
and the 9
), both methods fail, and produce 294.02
.
Here is a JSFiddle to demonstrate: https://jsfiddle.net/uj8x4khn/
My question: Is there a rounding method that will work on both of those numbers (and any number)?
Also curious: Why would it work on one number and not a very similar number?