PHP中round()函数对浮点数进行四舍五入的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-04-27 17:20:43 浏览: 评论:0
这篇文章主要介绍了PHP中round()函数对浮点数进行四舍五入的方法,通过详尽的实例对round()函数的各种常见用法进行了归纳整理,是非常实用的技巧,需要的朋友可以参考下
本文实例讲述了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程序设计有所帮助。
Tags: round PHP四舍五入
相关文章
- ·round() 函数对浮点数进行四舍五入(2014-09-19)
- ·php中的四舍五入函数代码(floor函数、ceil函数、round与intval)(2021-03-20)
- ·PHP四舍五入、取整、round函数使用示例(2021-05-10)
- ·php四舍五入保留两位小数点最精确的方法(2014-02-24)
- ·php浮点数四舍五入函数(2014-09-14)
- ·PHP四舍五入函数代码详解(2020-02-15)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)