超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<randsin>
Last updated: Fri, 22 Jun 2007

round

(PHP 3, PHP 4 , PHP 5)

round -- 对浮点数进行四舍五入

说明

float round ( float val [, int precision])

返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。

例子 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 默认不能正确处理类似 "12,300.2" 的字符串。见字符串转换为数值

注: precision 参数仅在 PHP 4 中可用。

参见 ceil()floor()number_format()




add a note add a note User Contributed Notes
round
twan at ecreation dot nl
16-May-2000 09:51
If you'd only want to round for displaying variables (not for calculating on the rounded result) then you should use printf with the float:
printf ("%6.2f",3.39532);

This returns: 3.40 .

<randsin>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22