round()
round() — Rounds a float.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns the rounded value of num to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).
Signature
round(int|float $num, int $precision = 0, int|RoundingMode $mode = RoundingMode::HalfAwayFromZero): float
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $num | int|float | The value to round. |
| $precision | int | The optional number of decimal digits to round to. |
| $mode | int|RoundingMode | Use RoundingMode or one of the following constants to specify the mode in which rounding occurs. Constants Description 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 ... |
Return value
The value rounded to the given precision as a float.
Example
A runnable snippet:
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));
Output:
float(3) float(4) float(4) float(4) float(5.05) float(5.06) float(300) float(0) float(700) float(1000)
Changelog
| Version | Change |
|---|---|
| 8.4.0 | Four new rounding modes have been added. |
| 8.4.0 | Now throws a ValueError if mode is invalid. |
| 8.0.0 | num no longer accepts internal objects which support numeric conversion. |
See also
ceil(), floor(), number_format()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page round() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.