Why 6.35.toFixed(1) == 6.3?
According to the documentation
toFixed both round to the nearest number:
0..4 lead down while
5..9 lead up.
In the similar example below, why is
6.35 rounded to
How to round
6.35 the right way?
Internally the decimal fraction
6.35 is an endless binary. As always in such cases, it is stored with a precision loss.
The precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that’s why it rounded down.
And what’s for
Here the precision loss made the number a little bit greater, so it rounded up.
How can we fix the problem with
6.35 if we want it to be rounded the right way?
We should bring it closer to an integer prior to rounding:
63.5 has no precision loss at all. That’s because the decimal part
0.5 is actually
1/2. Fractions divided by powers of
2 are exactly represented in the binary system, now we can round it: