<?php
//echo 1111.11*100;
//echo intval(1111.11*100);
if(1111.11*100 == 111111) {
echo 'that is right';
}else {
echo 'that is wrong';
}
printf('%.20f',1111.11*100);
?>
页面的输出结果是that is wrong
本文通过一个简单的PHP代码示例展示了在进行浮点数运算时可能出现的精度问题。通过对比预期结果与实际输出,解释了为何直接比较浮点数可能会得出错误结论,并提供了正确的输出结果。
<?php
//echo 1111.11*100;
//echo intval(1111.11*100);
if(1111.11*100 == 111111) {
echo 'that is right';
}else {
echo 'that is wrong';
}
printf('%.20f',1111.11*100);
?>
页面的输出结果是that is wrong
162
1196
4737
1565

被折叠的 条评论
为什么被折叠?