PHP integer rounding problems

后端 未结 5 1100
-上瘾入骨i
-上瘾入骨i 2021-01-13 08:31

echo (int) ( (0.1+0.7) * 10 );

Why does the above output 7? I understand how PHP rounds towards 0, but isn\'t (0.1+0.7) * 10 evaluated as

5条回答
  •  离开以前
    2021-01-13 08:59

    1/10 cannot be represented in a finite number of binary digits, just like 1/3 cannot be represented as a finite number of base-10 digits. Therefore you are actually adding together 0.09999999999999... and 0.69999999999999... -- the sum is almost 8, but not quite.

提交回复
热议问题