PHP round() Function

The round() function proves useful in rounding any floating point number up to a desired precision level. Positive precision parameter causes the number to be rounded after decimal point, whereas with negative precision, rounding occurs before decimal point. Precision is 0 by default.

For example, round(10.6) returns 11, round(10.2) returns 10. The function always returns a floating point number.

Syntax

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

Parameters

Parameter Description
value A float number to be rounded
precision Number of decimal digits to round to. Default is 0. Positive precision rounds after decimal point. Negative precision rounds before decimal point.
mode Rounding mode using predefined constants (see below)

Rounding Modes

Constant Description
PHP_ROUND_HALF_UP Rounds away from zero when halfway. 1.5 becomes 2, -1.5 becomes -2
PHP_ROUND_HALF_DOWN Rounds toward zero when halfway. 1.5 becomes 1, -1.5 becomes -1
PHP_ROUND_HALF_EVEN Rounds to the nearest even number
PHP_ROUND_HALF_ODD Rounds to the nearest odd number

Basic Examples

Here are basic rounding examples with different precision values ?

<?php
    echo "round(3.456) = " . round(3.456) . "<br>";
    echo "round(3.456, 1) = " . round(3.456, 1) . "<br>";
    echo "round(3.456, 2) = " . round(3.456, 2) . "<br>";
    echo "round(1234.5, -1) = " . round(1234.5, -1) . "<br>";
    echo "round(1234.5, -2) = " . round(1234.5, -2) . "<br>";
?>
round(3.456) = 3
round(3.456, 1) = 3.5
round(3.456, 2) = 3.46
round(1234.5, -1) = 1230
round(1234.5, -2) = 1200

Rounding Mode Examples

Different rounding modes affect how halfway values are handled ?

<?php
    echo "round(3.5, 0, PHP_ROUND_HALF_UP) = " . round(3.5, 0, PHP_ROUND_HALF_UP) . "<br>";
    echo "round(3.5, 0, PHP_ROUND_HALF_DOWN) = " . round(3.5, 0, PHP_ROUND_HALF_DOWN) . "<br>";
    echo "round(3.5, 0, PHP_ROUND_HALF_EVEN) = " . round(3.5, 0, PHP_ROUND_HALF_EVEN) . "<br>";
    echo "round(3.5, 0, PHP_ROUND_HALF_ODD) = " . round(3.5, 0, PHP_ROUND_HALF_ODD) . "<br>";
?>
round(3.5, 0, PHP_ROUND_HALF_UP) = 4
round(3.5, 0, PHP_ROUND_HALF_DOWN) = 3
round(3.5, 0, PHP_ROUND_HALF_EVEN) = 4
round(3.5, 0, PHP_ROUND_HALF_ODD) = 3

Return Values

PHP round() function returns a float number by rounding the value to desired precision.

Conclusion

The round() function is essential for controlling decimal precision in calculations. Use positive precision for decimal places and negative precision for rounding to tens, hundreds, etc.

Updated on: 2026-03-15T08:58:51+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements