PHP Reference ยท Math Functions
PHP round() function
Reference for PHP round(): round a number to the nearest integer or a number of decimal places, negative precision, rounding modes and floating-point pitfalls.
round() rounds a number to the nearest integer, or to a number of decimal places you choose. Halves are rounded away from zero by default: round(2.5) is 3 and round(-2.5) is -3. A negative precision rounds to the left of the decimal point, so round(1234, -2) gives 1200.
The function always returns a float. Remember that most decimal fractions cannot be stored exactly in binary floating point, so round() is a tool for presenting values. Use number_format() for output with separators and bcmath or integers (cents) for money arithmetic.
<?php echo round(3.14159, 2); // 3.14 echo round(1250, -2); // 1300
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
round(int|float $num, int $precision = 0, RoundingMode|int $mode = RoundingMode::HalfAwayFromZero): float
| Name | Description |
|---|---|
$num |
The value to round. |
$precision |
Digits after the decimal point. 0 rounds to a whole number; a negative value rounds to tens, hundreds and so on. |
$mode |
How to round exactly-half values: one of the RoundingMode enum cases (PHP 8.4+) or the constants PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, PHP_ROUND_HALF_ODD. |
Return value: the rounded value as a float.
Errors: a TypeError for non-numeric input, and a ValueError for an invalid mode.
Usage notes
- Default is "half away from zero", which is the usual school rule, not banker's rounding. For round-half-to-even use
RoundingMode::HalfEven. - Modes added in PHP 8.4: the
RoundingModeenum includes the four classic half modes plusTowardsZero,AwayFromZero,NegativeInfinityandPositiveInfinity, which round every value (not just halves) in that direction. The oldPHP_ROUND_*constants are still accepted but cover only the half modes. - Always a float, even for an
intargument (round(7)isfloat(7)). Cast with(int)if you need an integer. - Binary floating point. A value like
1.005is stored slightly below1.005, so rounding to two places can surprise you. PHP 8.4 fixed the old "pre-rounding" behavior, which made some results differ from earlier versions. - Display versus calculation. Keep full precision while calculating and round only at the end; rounding each step compounds the error.
- Truncating:
round()is not truncation. Usefloor()orceil(), or(int), for that. - Formatting:
round(5, 2)prints5; usenumber_format(5, 2)orsprintf("%.2f", 5)to show5.00.
Related
ceil()andfloor(),intdiv(),number_format(),sprintf(),abs()- Lessons: Math functions, Numbers