为什么65.6 * 100%10等于9,而不是PHP中的0? [英] Why does 65.6*100%10 equals to 9, instead of 0 in PHP?
本文介绍了为什么65.6 * 100%10等于9,而不是PHP中的0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
echo 65.7 * 100 % 10 // 0
echo 65.6 * 100 % 10 // 9 <---
echo 6560 % 10 // 0
echo 65.5 * 100 % 10 // 0
有人可以解释为什么吗?
Can someone please explain why?
对于人类或非程序员,结果9显然是错误的.
for human or non-programmers, the result 9 is apprently wrong.
在编程时如何防止此"error"
?
推荐答案
因为浮点中的65.6
实际上是65.6
的近似值.实际上实际上小于65.6
,即65.5999
或类似值.
Because 65.6
in floating point is actually an approximation of 65.6
. It is actually marginally less than 65.6
, i.e. 65.5999
or similar.
Assuming my wild guess at the actual value is correct, you have 6559.9
, which is stripped to 6599
by the modulus operator, then divided by 10 for remainder 9.
这篇关于为什么65.6 * 100%10等于9,而不是PHP中的0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文