php 中四舍五入的方法

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


php 中四舍五入的方法


php round() 函数

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

语法:

round(x,prec)

参数:

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

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

PHP 四舍五入的方法

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

php代码:

echo(round(0.66));
echo '
';
echo(round(0.44));
echo '
';
echo(round(0.49));
echo '
';
echo(round(-4.62));
echo '
';
echo(round(-4.30));

运行结果:

1
0
0
-5
-4

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

php代码:

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

运行结果:

0.66
0.44234
0.5
-4.624566
-4.307


分享到:


相關文章: