- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
|
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
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
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
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
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