您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章给大家分享的是有关如何在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中利用round()函数实现一个四舍五入功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。