先看個例子
<?php
$user_money = round('9.12'*100);
echo ('9.12'*100)."\r\n";
//捨去法取整
echo (floor('9.12'*100)."\r\n");
//四捨五入
echo (round('9.12'*100)."\r\n");
$fee = 912;
if($fee > $user_money)
{
echo ($fee - $user_money);
echo ($fee.' '.$user_money);
}
?>
這裏9.12*100以後得到的結果是912。但是在用捨去法取整後得到的是911.說明內部儲存沒有使用912來儲存應該是911.xxxx
經過四捨五入以後得到912,
在這裏下面的對比可以看出在沒有使用round()方法進行四捨五入法取整前,$fee>$user_money成立,
爲了避免這個坑,建議在float取整的過程中要用round方法進行轉換一下,