我有这段代码:
$sum=0;
while($row=oci_fetch_row($que))
{
$sum+=$row[8];
}
echo number_format((float)$sum,2,",","").'<br>';
有3行,其值为&#39; 7.01&#39; &#39; 43.76&#39; &#39; 11.64&#39; echo打印61.00而不是62.41。 为什么? 对应的mySql代码就像一个魅力...... 解决了..... How to convert a string to float with "tail"? 谢谢你们的时间
答案 0 :(得分:1)
浮点运算。请参阅Floating point precision和Why don’t my numbers add up?
echo serialize(7.01);
echo serialize(43.76);
echo serialize(11.64);;
首先尝试BC Match Functions或舍入到2位小数:
echo round(7.01, 2) + round(43.76, 2) + round(11.64, 2);
//$sum += round($row[8], 2);