PHP计算浮点数 [英] php calculate float
本文介绍了PHP计算浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有一个奇怪的数学计算.我希望有人能解释.
I have a weird math calculation here. I hope someone will explain.
$a = 1.85/100;
$b = 1.5/100;
$c = 1.1/100;
$d = 0.4/100;
$e = 0.4/100;
$f = 0.4/100;
$g = 0.4/100;
$h = $a + $b + $c + $d + $e + $f + $g;
echo $h*100 ."<br>";
$i = $h-$a;
$i = $i-$b;
$i = $i-$c;
$i = $i-$d;
$i = $i-$e;
$i = $i-$f;
$i = $i-$g;
echo $i;
最后一个$i
值应为0,但返回6.93889390391E-18.
The last $i
value should be 0 but it returns 6.93889390391E-18.
推荐答案
有什么办法纠正我的方程式以显示最终结果0.00吗?
any idea to correct my equation to show final result 0.00 ?
是的,round($i, 2)
差异"通常很小,以至于将其四舍五入到小数点后两位几乎总是可以解决问题.
The "discrepancies" are usually so small, that rounding it to 2 decimals will almost always solve the problem.
这篇关于PHP计算浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文