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.cuoshuo.com/blog/4437.html