Rounding In Javascript Tofixed() Method
Solution 1:
As stated in the docs, toFixed()
does round when necessary. The rounding behavior is to round in the range -.5 < x <= +.5 of the digit.
The strange behavior you're observing is consistent with the note in the docs linked above:
Floating point numbers cannot represent all decimals precisely in binary which can lead to unexpected results such as 0.1 + 0.2 === 0.3 returning false .
In other words, this is a classic case of floating point precision loss - a problem you'll encounter in virtually any language. If you observe the full outputs of a
and b
you'll see that a == 0.075
and b == 0.07500000000000001
due to floating point precision - and thus given these values it is consistent with the defined rounding behavior to round a
to .07
and b
to .08
.
Solution 2:
The problem you are encountering is not specific to JavaScript, it is common to computing in general.
Both these arithmetic calculations have the same result – 0.075:
- 0.25 * 0.3 = 0.075
- 0.025 * 3 = 0.075
This is using the decimal number system commonly used.
Computers, at their core, however, don't use the decimal system, but binary – everything is based on 0 and 1.
Because of this, they actually have a hard time getting the calculation above right. JavaScript and other programming languages have to approximate the result, giving you this:
- 0.25 * 0.3 = 0.75
- 0.025 * 3 = 0.07500000000000001
You can now see why toFixed
returns different results:
- 0.75.toFixed(2) = 0.07
- 0.07500000000000001.toFixed(2) = 0.08
Post a Comment for "Rounding In Javascript Tofixed() Method"