Woooowen You can do anything you set your mind to, man

php 浮点运算问题

关于浮点数的问题,请看http://www.laruence.com/2013/03/26/2884.html

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));

Woooowen 浙ICP备15013647号