您的位置 首页 php

php 中四舍五入的方法

php中在处理带在多位小数的数字时,有时候要用到四舍五入的方法取这个数字的相似值。那么这篇博文就来介绍一下,php 中四舍五入的函数 round()

php round() 函数

round():对于一个带有小数的数字进行四舍五入。

语法:

round(x,prec) 

参数:

x:(可选)可四舍五入处理的数字

prec:(可选)规定要保留的小数的位数,它可以是负数或零(默认)

PHP 四舍五入的方法

1、使用 round() 函数进行四舍五入

php代码:

echo(round(0.66));
echo '<br/>';
echo(round(0.44));
echo '<br/>';
echo(round(0.49));
echo '<br/>';
echo(round(-4.62));
echo '<br/>';
echo(round(-4.30)); 

运行结果:

1
0
0
-5
-4 

2、自定义要保留小数的位数

php代码:

echo(round(0.662435,2));
echo '<br/>';
echo(round(0.442341,5));
echo '<br/>';
echo(round(0.49,1));
echo '<br/>';
echo(round(-4.62456566,6));
echo '<br/>';
echo(round(-4.306659,3)); 

运行结果:

0.66
0.44234
0.5
-4.624566
-4.307
 

文章来源:智云一二三科技

文章标题:php 中四舍五入的方法

文章地址:https://www.zhihuclub.com/78056.shtml

关于作者: 智云科技

热门文章

网站地图