Welcome to the world of IEEE 754.
0.1 + 0.2
is
. You have to use Math.round()
, Number.toFixed()
, Number.toPrecision()
or something like that to avoid this.
0.1 + 0.2 - 0.3
is
.
(0.1 + 0.2) - 0.3
is
.
0.1 + (0.2 - 0.3)
is
.
echo 0.1 + 0.2;
is 0.3
. Why? Because PHP displays only a few decimal places by default.
printf("%.17f", 0.1 + 0.2);
is 0.30000000000000004
.
echo 0.1 + 0.2 - 0.3;
is 5.5511151231258E-17
.
echo (0.1 + 0.2) - 0.3;
is 5.5511151231258E-17
.
echo 0.1 + (0.2 - 0.3);
is 2.7755575615629E-17
.