时间:2021-05-26
本文实例讲述了PHP中round()函数对浮点数进行四舍五入的方法。分享给大家供大家参考。具体方法如下:
语法:round(x,prec)
参数 描述 x 可选,规定要舍入的数字. prec 可选,规定小数点后的位数.说明:返回将 x 根据指定精度 prec(十进制小数点后数字的数目)进行四舍五入的结果,prec 也可以是负数或零(默认值).
提示和注释
注释:php默认不能正确处理类似 "12,300.2" 的字符串.
注释:prec 参数是在 php 4 中被引入的,实例代码如下:
复制代码 代码如下:<?php
*/
echo round(3.4); //输出3
echo "<br>";
echo round(3.5); //输出4
echo "<br>";
echo round(3.6); //输出4
echo "<br>";
echo round(3.6,0); //输出4
echo "<br>";
echo round(1.95583,2); //输出1.96
echo "<br>";
echo round(1241757,-3); //输出1242000
echo "<br>";
echo round(4.045,2); //输出4.05
echo "<br>";
echo round(4.055,2); //输出4.06
//使用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 ";
?>
希望本文所述对大家的PHP程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
一:四舍五入1.round—对浮点数进行四舍五入floatround(float$val[,int$precision])2:floor—舍去法取整(向下取整)
roundround-对浮点数进行四舍五入。round函数语法如下:round(float,precision)其中参数precision表示小数点后面要保持的
代码如下所示:$num=10.4567;//第一种:利用round()对浮点数进行四舍五入echoround($num,2);//10.46//第二种:利用sp
round()函数简介菜鸟教程中介绍到,round()函数作用就是,返回浮点数x的四舍五入值。>round(x[,n])参数x,n均为数值表达式,返回值为x的四
本文实例讲述了MySQL中ROUND函数进行四舍五入操作陷阱。分享给大家供大家参考,具体如下:在MySQL中,ROUND函数用于对查询结果进行四舍五入,不过最近