Skip to content
Tutorials Center
My learning

Topics

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
<?php
echo round(3.14159, 2);   // 3.14
echo round(1250, -2);     // 1300

Try it

PHP Precision, halves and negative precision

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.

PHP Rounding modes

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

PHP
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 RoundingMode enum includes the four classic half modes plus TowardsZero, AwayFromZero, NegativeInfinity and PositiveInfinity, which round every value (not just halves) in that direction. The old PHP_ROUND_* constants are still accepted but cover only the half modes.
  • Always a float, even for an int argument (round(7) is float(7)). Cast with (int) if you need an integer.
  • Binary floating point. A value like 1.005 is stored slightly below 1.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. Use floor() or ceil(), or (int), for that.
  • Formatting: round(5, 2) prints 5; use number_format(5, 2) or sprintf("%.2f", 5) to show 5.00.