PHP round() Function

Definition and Usage

The round() function proves useful in rounding any floating point number upto 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.

This function also has another optional parameter called mode takes one of the redefined constants described later.

Syntax

round ( float $value , int $precision , int $mode ) : float

Parameters

Sr.No Parameter & Description
1 value
A float number to be rounded
2 precision
number of decimal digits to round to. Default is 0. Positive precision rounds given number after decimal point. Negative precision rounds the given number before decimal point.
3 mode
one of the following predefined constants
PHP_ROUND_HALF_UP rounds number away from 0 when it is half way there. Hence, 1.5 becomes 2 and -1.5 to -2
PHP_ROUND_HALF_DOWN

rounds number towards 0 when it is half way there. Hence 1.5 becomes 1 and -1.5 to -1
PHP_ROUND_HALF_EVEN rounds the number to nearest even value
PHP_ROUND_HALF_ODD rounds the number to nearest odd value

Return Values

PHP round() function returns a float number thatby rounding the value todesired precision.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

<?php
   echo "round( 3.45,HALF_ODD) = " . round(3.45,0, PHP_ROUND_HALF_ODD) . "
";    echo "round(3.78 HALF_EVEN) = " . round(3.78, 0, PHP_ROUND_HALF_EVEN) . "
"; ?>

Output

This will produce following result −

round( 3.45,HALF_ODD) = 3
round(3.78, HALF_EVEN) = 4
Updated on: 2026-03-11T23:22:53+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements