(PHP 4, PHP 5, PHP 7, PHP 8)
round — 对浮点数进行四舍五入
$num
, int $precision
= 0, int $mode
= PHP_ROUND_HALF_UP
): float
返回将 num
根据指定精度
precision
(十进制小数点后数字的数目)进行四舍五入的结果。precision
也可以是负数或零(默认值)。
num
要处理的值。
precision
可选的十进制小数点后数字的数目。
如果 precision
是正数,则 num
会四舍五入到小数点后 precision
位有效数字。
如果 precision
是负数,则 num
四舍五入到小数点前 precision
位有效数字。即 pow(10,
-precision)
最接近的倍数,例如,precision
为 -1,num
可以四舍五入到十位,precision
为 -2,num
可以四舍五入到百位,等等。
mode
使用以下常量指定四舍五入发生的模式。
常量 | 说明 |
---|---|
PHP_ROUND_HALF_UP |
Rounds num away from zero when it is half way there,
making 1.5 into 2 and -1.5 into -2.
|
PHP_ROUND_HALF_DOWN |
Rounds num towards zero when it is half way there,
making 1.5 into 1 and -1.5 into -1.
|
PHP_ROUND_HALF_EVEN |
Rounds num towards the nearest even value when it is half way
there, making both 1.5 and 2.5 into 2.
|
PHP_ROUND_HALF_ODD |
Rounds num towards the nearest odd value when it is half way
there, making 1.5 into 1 and 2.5 into 3.
|
值是四舍五入到指定 precision
精度的 float。
版本 | 说明 |
---|---|
8.0.0 |
num 不再接受支持数字转换的内部对象。
|
示例 #1 round() 示例
<?php
var_dump(round(3.4));
var_dump(round(3.5));
var_dump(round(3.6));
var_dump(round(3.6, 0));
var_dump(round(5.045, 2));
var_dump(round(5.055, 2));
var_dump(round(345, -2));
var_dump(round(345, -3));
var_dump(round(678, -2));
var_dump(round(678, -3));
?>
以上例程会输出:
float(3) float(4) float(4) float(4) float(5.05) float(5.06) float(300) float(0) float(700) float(1000)
示例 #2 precision
如何影响 float
<?php
$number = 135.79;
var_dump(round($number, 3));
var_dump(round($number, 2));
var_dump(round($number, 1));
var_dump(round($number, 0));
var_dump(round($number, -1));
var_dump(round($number, -2));
var_dump(round($number, -3));
?>
以上例程会输出:
float(135.79) float(135.79) float(135.8) float(136) float(140) float(100) float(0)
示例 #3 mode
示例
<?php
echo 'Rounding modes with 9.5' . PHP_EOL;
var_dump(round(9.5, 0, PHP_ROUND_HALF_UP));
var_dump(round(9.5, 0, PHP_ROUND_HALF_DOWN));
var_dump(round(9.5, 0, PHP_ROUND_HALF_EVEN));
var_dump(round(9.5, 0, PHP_ROUND_HALF_ODD));
echo PHP_EOL;
echo 'Rounding modes with 8.5' . PHP_EOL;
var_dump(round(8.5, 0, PHP_ROUND_HALF_UP));
var_dump(round(8.5, 0, PHP_ROUND_HALF_DOWN));
var_dump(round(8.5, 0, PHP_ROUND_HALF_EVEN));
var_dump(round(8.5, 0, PHP_ROUND_HALF_ODD));
?>
以上例程会输出:
Rounding modes with 9.5 float(10) float(9) float(10) float(9) Rounding modes with 8.5 float(9) float(8) float(8) float(9)
示例 #4 带 precision
的 mode
示例
<?php
echo 'Using PHP_ROUND_HALF_UP with 1 decimal digit precision' . PHP_EOL;
var_dump(round( 1.55, 1, PHP_ROUND_HALF_UP));
var_dump(round(-1.55, 1, PHP_ROUND_HALF_UP));
echo PHP_EOL;
echo 'Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision' . PHP_EOL;
var_dump(round( 1.55, 1, PHP_ROUND_HALF_DOWN));
var_dump(round(-1.55, 1, PHP_ROUND_HALF_DOWN));
echo PHP_EOL;
echo 'Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision' . PHP_EOL;
var_dump(round( 1.55, 1, PHP_ROUND_HALF_EVEN));
var_dump(round(-1.55, 1, PHP_ROUND_HALF_EVEN));
echo PHP_EOL;
echo 'Using PHP_ROUND_HALF_ODD with 1 decimal digit precision' . PHP_EOL;
var_dump(round( 1.55, 1, PHP_ROUND_HALF_ODD));
var_dump(round(-1.55, 1, PHP_ROUND_HALF_ODD));
?>
以上例程会输出:
Using PHP_ROUND_HALF_UP with 1 decimal digit precision float(1.6) float(-1.6) Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision float(1.5) float(-1.5) Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision float(1.6) float(-1.6) Using PHP_ROUND_HALF_ODD with 1 decimal digit precision float(1.5) float(-1.5)