PHP round

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : round() function

Description

The round() function is used to round a floating-point number.

Version

(PHP 4 and above)

Syntax

round (num1, num2, mode)

Parameters

Name

Description

Required /
Optional

Type

num1

The numeric expression whose value is to be rounded.

Required

Float

num2

The number of decimal digits to round to.

Optional

Integer

mode

Rounding mode.
Possible values :
PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, PHP_ROUND_HALF_ODD.

Optional

Integer

Return value

The rounded value.

Value Type : Float.

Pictorial Presentation

php-math-round()

Example :

  1. $round1=round(13.4);  

  2. $round2=round(13.5);  

  3. $round3=round(13.6);  

  4. $round4=round(13.6,0);  

  5. $round5=round(5.85543,2);  

  6. $round6=round(5.85543,-3);  

  7. $round7=round(6.054,2);  

  8. $round8=round(6.066,2);  

  9. echo "Round of (13,4)  is : ". $round1 . "
    " ;  

  10. echo "Round of (13,5)  is : ". $round2 . "
    " ;  

  11. echo "Round of (13,6)  is : ". $round3 . "
    " ;  

  12. echo "Round of (13.6,0)  is : ". $round4 . "
    " ;  

  13. echo "Round of (5.85543,2)  is : ". $round5 . "
    " ;  

  14. echo "Round of (5.85543,-3) is : ". $round6 . "
    " ;  

  15. echo "Round of (6.054,2)  is : ". $round7 . "
    " ;  

  16. echo "Round of (6.066,2)  is : ". $round8 . "
    " ;  

  17. ?>  

Output :

Round of (13,4) is : 13
Round of (13,5) is : 14
Round of (13,6) is : 14
Round of (13.6,0) is : 14
Round of (5.85543,2) is : 5.86
Round of (5.85543,-3) is : 0
Round of (6.054,2) is : 6.05
Round of (6.066,2) is : 6.07