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.NoParameter & Description
1value
A float number to be rounded
2precision
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.
3mode
one of the following predefined constants
PHP_ROUND_HALF_UProunds 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_EVENrounds the number to nearest even value
PHP_ROUND_HALF_ODDrounds 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

 Live Demo

Following example rounds given number to positive precision values −

<?php
   $arg=1234.567;
   echo "round(" . $arg . ") = " . round($arg) . "
";    echo "round(" . $arg . ",1) = " . round($arg,1) . "
";    echo "round(" . $arg . ",2) = " . round($arg,2) . "
"; ?>

Output

This will produce following result −

round(1234.567) = 1235
round(1234.567,1) = 1234.6
round(1234.567,2) = 1234.57

Example

 Live Demo

Following example rounds the number to negative precision values −

<?php
   $arg=1234.567;
   echo "round(" . $arg . ") = " . round($arg) . "
";    echo "round(" . $arg . ",-1) = " . round($arg,-1) . "
";    echo "round(" . $arg . ",-2) = " . round($arg,-2) . "
"; ?>

Output

This will produce following result −

round(1234.567) = 1235
round(1234.567,-1) = 1230
round(1234.567,-2) = 1200

Example

 Live Demo

Following example uses UP and DOWN mode constants for rounding −

<?php
echo "round( 3.45,HALF_UP) = " . round(3.45,0, PHP_ROUND_HALF_UP) . "
"; echo "round(3.75 HALF_UP) = " . round(3.75, 1, PHP_ROUND_HALF_DOWN) . "
"; ?>

Output

This will produce following result −

round( 3.45,HALF_UP) = 3
round(3.75 HALF_UP) = 3.7

Example

 Live Demo

Following example uses ODD and EVEN modes for rounding

<?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: 30-Jun-2020

693 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements