fucker 2014-12-11 14:09:59

PHP的浮点数运算一直存在问题.

example1:
$a = floor((0.1+0.7) * 10);
//返回的结果并不是8,而是7
echo $a;

---------------------------------------
so use round()
$a = round((0.1+0.7) * 10);
//返回的结果 = 8
echo $a;

example2:
$a = 9 - 5.1;
$b = 3.9;
var_dump($a == $b);
//返回结果是false
---------------------------------------
so use round()
$a = 9 - 5.1;
$b = 3.9;
var_dump(round($a, 2) == round($b, 2));

或者你也可以使用gmp或者bc之类的高精度函数来运算

评论

ホーム - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-15 00:33
浙ICP备14020137号-1 $お客様$