代码:
<?php
echo "round(5.005,2)= ".round(5.005,2). "\n ";
echo "round(5.015,2)= ".round(5.015,2). "\n ";
echo "round(5.025,2)= ".round(5.025,2). "\n ";
echo "round(5.035,2)= ".round(5.035,2). "\n ";
echo "round(5.045,2)= ".round(5.045,2). "\n ";
echo "round(5.055,2)= ".round(5.055,2). "\n ";
echo "round(5.065,2)= ".round(5.065,2). "\n ";
echo "round(5.075,2)= ".round(5.075,2). "\n ";
echo "round(5.085,2)= ".round(5.085,2). "\n ";
echo "round(5.095,2)= ".round(5.095,2). "\n ";
$m=0.000000000000001;
echo "\n ";
echo "round(5.005+{$m},2)= ".round(5.005+$m,2). "\n ";
echo "round(5.015+{$m},2)= ".round(5.015+$m,2). "\n ";
echo "round(5.025+{$m},2)= ".round(5.025+$m,2). "\n ";
echo "round(5.035+{$m},2)= ".round(5.035+$m,2). "\n ";
echo "round(5.045+{$m},2)= ".round(5.045+$m,2). "\n ";
echo "round(5.055+{$m},2)= ".round(5.055+$m,2). "\n ";
echo "round(5.065+{$m},2)= ".round(5.065+$m,2). "\n ";
echo "round(5.075+{$m},2)= ".round(5.075+$m,2). "\n ";
echo "round(5.085+{$m},2)= ".round(5.085+$m,2). "\n ";
echo "round(5.095+{$m},2)= ".round(5.095+$m,2). "\n ";
?>
结果:
round(5.005,2)=5.01
round(5.015,2)=5.01
round(5.025,2)=5.03
round(5.035,2)=5.04
round(5.045,2)=5.05
round(5.055,2)=5.06
round(5.065,2)=5.07
round(5.075,2)=5.08
round(5.085,2)=5.09
round(5.095,2)=5.1
round(5.005+1E-015,2)=5.01
round(5.015+1E-015,2)=5.02
round(5.025+1E-015,2)=5.03
round(5.035+1E-015,2)=5.04
round(5.045+1E-015,2)=5.05
round(5.055+1E-015,2)=5.06
round(5.065+1E-015,2)=5.07
round(5.075+1E-015,2)=5.08
round(5.085+1E-015,2)=5.09
round(5.095+1E-015,2)=5.1
PHP手册:
例子 1. round() 例子
<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.04
echo round(5.055, 2); // 5.06
?>
注意
在对正好一半的分数进行舍入时 round() 在偶数次舍去,在奇数次进位。如果你想迫使它对 .5(或 .05)总向一个方向舍入,加上或减去一个很小的因子。对一半值舍去对另一半值进一的背后原因是避免传统的银行问题,即如果总是舍去则银行从顾客那里多拿了钱,如果总是进一则银行最终会损失了钱。对此平均舍入则在统计上达到了平衡。
PHP手册中
echo round(5.045, 2); // 5.04
echo round(5.055, 2); // 5.06
而我代码的结果却是:
round(5.045,2)=5.05
round(5.055,2)=5.06
round(5.005,2)=5.01
round(5.015,2)=5.01
round(5.025,2)=5.03
round(5.035,2)=5.04
round(5.045,2)=5.05
round(5.055,2)=5.06
从这个结果看,也不明白 "在偶数次舍去,在奇数次进位 "是一个什么样的原则,
请各位PHP强人指教.
===================================================================================================
从php.net上找到一个比较合理的方案:
使用stdround函数代替php的round函数
<?php
function stdround($num, $d=0)
{
return round($num + 0.0001 / pow(10, $d), $d);
}
echo "round(1.005,2)= ".round(1.005,2). "\n ";
echo "round(1.015,2)= ".round(1.015,2). "\n ";
echo "round(1.025,2)= ".round(1.025,2). "\n ";
echo "round(1.035,2)= ".round(1.035,2). "\n ";
echo "round(1.045,2)= ".round(1.045,2). "\n ";
echo "round(1.055,2)= ".round(1.055,2). "\n ";
echo "round(1.065,2)= ".round(1.065,2). "\n ";
echo "round(1.075,2)= ".round(1.075,2). "\n ";
echo "round(1.085,2)= ".round(1.085,2). "\n ";
echo "round(1.095,2)= ".round(1.095,2). "\n ";
echo "stdround(1.005,2)= ".stdround(1.005,2). "\n ";
echo "stdround(1.015,2)= ".stdround(1.015,2). "\n ";
echo "stdround(1.025,2)= ".stdround(1.025,2). "\n ";
echo "stdround(1.035,2)= ".stdround(1.035,2). "\n ";
echo "stdround(1.045,2)= ".stdround(1.045,2). "\n ";
echo "stdround(1.055,2)= ".stdround(1.055,2). "\n ";
echo "stdround(1.065,2)= ".stdround(1.065,2). "\n ";
echo "stdround(1.075,2)= ".stdround(1.075,2). "\n ";
echo "stdround(1.085,2)= ".stdround(1.085,2). "\n ";
echo "stdround(1.095,2)= ".stdround(1.095,2). "\n ";
$m=0.000000000000001;
echo "\n ";
echo "round(1.005+{$m},2)= ".round(1.005+$m,2). "\n ";
echo "round(1.015+{$m},2)= ".round(1.015+$m,2). "\n ";
echo "round(1.025+{$m},2)= ".round(1.025+$m,2). "\n ";
echo "round(1.035+{$m},2)= ".round(1.035+$m,2). "\n ";
echo "round(1.045+{$m},2)= ".round(1.045+$m,2). "\n ";
echo "round(1.055+{$m},2)= ".round(1.055+$m,2). "\n ";
echo "round(1.065+{$m},2)= ".round(1.065+$m,2). "\n ";
echo "round(1.075+{$m},2)= ".round(1.075+$m,2). "\n ";
echo "round(1.085+{$m},2)= ".round(1.085+$m,2). "\n ";
echo "round(1.095+{$m},2)= ".round(1.095+$m,2). "\n ";
echo "round(1.005,2)= ".round(1.005,2). "\n ";
echo "round(1.015,2)= ".round(1.015,2). "\n ";
echo "round(1.025,2)= ".round(1.025,2). "\n ";
echo "round(1.035,2)= ".round(1.035,2). "\n ";
echo "round(1.045,2)= ".round(1.045,2). "\n ";
echo "round(1.055,2)= ".round(1.055,2). "\n ";
echo "round(1.065,2)= ".round(1.065,2). "\n ";
echo "round(1.075,2)= ".round(1.075,2). "\n ";
echo "round(1.085,2)= ".round(1.085,2). "\n ";
echo "round(1.095,2)= ".round(1.095,2). "\n ";
?>
返回结果:
round(1.005,2)=1
round(1.015,2)=1.01
round(1.025,2)=1.02
round(1.035,2)=1.03
round(1.045,2)=1.05
round(1.055,2)=1.06
round(1.065,2)=1.07
round(1.075,2)=1.08
round(1.085,2)=1.09
round(1.095,2)=1.1
stdround(1.005,2)=1.01
stdround(1.015,2)=1.02
stdround(1.025,2)=1.03
stdround(1.035,2)=1.04
stdround(1.045,2)=1.05
stdround(1.055,2)=1.06
stdround(1.065,2)=1.07
stdround(1.075,2)=1.08
stdround(1.085,2)=1.09
stdround(1.095,2)=1.1
round(1.005+1E-015,2)=1.01
round(1.015+1E-015,2)=1.02
round(1.025+1E-015,2)=1.03
round(1.035+1E-015,2)=1.04
round(1.045+1E-015,2)=1.05
round(1.055+1E-015,2)=1.06
round(1.065+1E-015,2)=1.07
round(1.075+1E-015,2)=1.08
round(1.085+1E-015,2)=1.09
round(1.095+1E-015,2)=1.1
round(1.005,2)=1
round(1.015,2)=1.01
round(1.025,2)=1.02
round(1.035,2)=1.03
round(1.045,2)=1.05
round(1.055,2)=1.06
round(1.065,2)=1.07
round(1.075,2)=1.08
round(1.085,2)=1.09
round(1.095,2)=1.1