round
Rounds a number to a given number of digits.
Available since: PHP 4
Syntax
round(int|float $num, int $precision = 0): floatParameters
| Parameter | Type | Description |
|---|---|---|
| $num | int|float | The number to round. |
| $precision | int | How many decimal places to keep. A negative value rounds to tens, hundreds and so on. |
Return value
floatThe rounded number.
Examples
To two decimal places:
<?php
echo round(3.14159, 2);Output:
3.14A half is rounded away from zero:
<?php
echo round(2.5), "\n";
echo round(-2.5);Output:
3
-3Rounding to hundreds:
<?php
echo round(1234, -2);Output:
1200Notes
The result is always a float, even when rounding to a whole number. A third argument selects a different rounding mode.
See also
Related recipes
In the tutorial
- Operators — Arithmetic, comparison and logical operators in PHP, and operator precedence.