PHP Round()函数
round()函数用于四舍五入一个浮点数。它有三个参数并返回四舍五入的值。
语法
float round(number,precision, $mode);
参数 | 描述 | 可选/必需 |
---|---|---|
number | 指定要舍入的值 | 必需 |
Precision | 指定要舍入到的小数位数 | 可选 |
Mode | 指定舍入发生的模式。 | 可选 |
- PHP_ROUND_HALF_UP
- PHP_ROUND_HALF_DOWN
- PHP_ROUND_HALF_EVEN
- PHP_ROUND_HALF_ODD
示例1
<?php
echo "Befor using round() function : 3.96754,2";
echo "<br>"."After using round() function : ".(round(3.96754,2));
?>
输出:
Befor using round() function : 3.96754,2
After using round() function : 3.97
示例2
<?php
echo "Befor using round() function : 7.045,2";
echo "<br>"."After using round() function : ".(round(7.045,2));
?>
输出:
Befor using round() function : 7.045,2
After using round() function : 7.05
示例3
<?php
echo "Befor using round() function : 7.055,2";
echo "<br>"."After using round() function : ".(round(7.055,2));
?>
输出:
Befor using round() function : 7.055,2
After using round() function : 7.06
示例4
<?php
echo(round(1.6,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(-1.6,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(1.6,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(-1.6,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(1.6,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(-1.6,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(1.6,0,PHP_ROUND_HALF_ODD) . "<br>");
echo(round(-1.6,0,PHP_ROUND_HALF_ODD));
?>
输出:
2
-2
1
-1
2
-2
1
-1