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
Example:
<?php
$round1=round(13.4);
$round2=round(13.5);
$round3=round(13.6);
$round4=round(13.6,0);
$round5=round(5.85543,2);
$round6=round(5.85543,-3);
$round7=round(6.054,2);
$round8=round(6.066,2);
echo "Round of (13,4) is : ". $round1 . "<br />" ;
echo "Round of (13,5) is : ". $round2 . "<br />" ;
echo "Round of (13,6) is : ". $round3 . "<br />" ;
echo "Round of (13.6,0) is : ". $round4 . "<br />" ;
echo "Round of (5.85543,2) is : ". $round5 . "<br />" ;
echo "Round of (5.85543,-3) is : ". $round6 . "<br />" ;
echo "Round of (6.054,2) is : ". $round7 . "<br />" ;
echo "Round of (6.066,2) is : ". $round8 . "<br />" ;
?>
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
View the example in the browser
See also
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php/function-reference/round.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics