This is the expression I'm having trouble with:
Math.round((Math.ceil((0.4 - 0.1 / 1000) / 0.1) - 1)) * 0.1
In both Chrome and Firefox, it returns 0.30000000000000004. This number needs to be put back into an <input type="number">
element. While Firefox is smart enough to write "0.3", Chrome presents the user the full beauty of JavaScript's inability to do maths properly. Needless to say this is unacceptable from a user's perspective.
Only I can't find a way to make it work correctly. I know that JavaScript is very bad at calculating, but there has to be a solution. I almost don't care about how complicated it is, but the user needs to see "0.3", not a number with 16 decimals when the step is 0.1.
I'd expect – and I've often used it – for Math.round(x / scale) * scale
to return a number that is properly rounded to lg(scale) digits. But this has started to fail now.
What magic code is necessary to defeat this evil spell?
PS: The step of 0.1 is an example. It could be anything the user desires. Like 1, 10, 12, 0.25 or 0.5.