round

(PHP 4, PHP 5, PHP 7, PHP 8)

round对浮点数进行四舍五入

说明

round ( float $val , int $precision = 0 , int $mode = PHP_ROUND_HALF_UP ) : float

返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。

参数

val

要处理的值。

precision

可选的十进制小数点后数字的数目。

If the precision is positive, val is rounded to precision significant digits after the decimal point.

If the precision is negative, val is rounded to precision significant digits before the decimal point, i.e. to the nearest multiple of pow(10, -precision), e.g. for a precision of -1 val is rounded to tens, for a precision of -2 to hundreds, etc.

mode

Use one of the following constants to specify the mode in which rounding occurs.

常量 说明
PHP_ROUND_HALF_UP Rounds val away from zero when it is half way there, making 1.5 into 2 and -1.5 into -2.
PHP_ROUND_HALF_DOWN Rounds val towards zero when it is half way there, making 1.5 into 1 and -1.5 into -1.
PHP_ROUND_HALF_EVEN Rounds val 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 val towards the nearest odd value when it is half way there, making 1.5 into 1 and 2.5 into 3.

返回值

The value rounded to the given precision as a float.

范例

示例 #1 round() 例子

<?php
var_dump
(round(3.4));
var_dump(round(3.5));
var_dump(round(3.6));
var_dump(round(3.60));
var_dump(round(5.0452));
var_dump(round(5.0552));
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 How precision affects a float

<?php
$number 
135.79;

var_dump(round($number3));
var_dump(round($number2));
var_dump(round($number1));
var_dump(round($number0));
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.50PHP_ROUND_HALF_UP));
var_dump(round(9.50PHP_ROUND_HALF_DOWN));
var_dump(round(9.50PHP_ROUND_HALF_EVEN));
var_dump(round(9.50PHP_ROUND_HALF_ODD));

echo 
PHP_EOL;
echo 
'Rounding modes with 8.5' PHP_EOL;
var_dump(round(8.50PHP_ROUND_HALF_UP));
var_dump(round(8.50PHP_ROUND_HALF_DOWN));
var_dump(round(8.50PHP_ROUND_HALF_EVEN));
var_dump(round(8.50PHP_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 mode with precision examples

<?php
echo 'Using PHP_ROUND_HALF_UP with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_UP));
var_dump(round(-1.551PHP_ROUND_HALF_UP));

echo 
PHP_EOL;
echo 
'Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_DOWN));
var_dump(round(-1.551PHP_ROUND_HALF_DOWN));

echo 
PHP_EOL;
echo 
'Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_EVEN));
var_dump(round(-1.551PHP_ROUND_HALF_EVEN));

echo 
PHP_EOL;
echo 
'Using PHP_ROUND_HALF_ODD with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_ODD));
var_dump(round(-1.551PHP_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)

参见