Home >>PHP Math Functions >PHP round() Function

PHP round() Function

PHP round() Function

PHP round() function is used to round any given input floating-point number. It is used to define a specific precision value which rounds the given number according to that precision value. Precision value can be negative or zero also. It accepts 3 3 parameters which are $number, $precision, and $mode among which only $number is a required parameter and other two are optional parameters.

Syntax:

  round($number,$precision,$mode);

Parameter Values

Parameter Description
number This is a required parameter. This parameter defines the value to round.
precision This is an optional parameter. This parameter defines the number of decimal digits to round to.
mode This is an optional parameter. This parameter defines a constant to specify the mode.

Here is an example of round() function in PHP:

<html>
<body>

<?php

echo round(0.21)."<br>";
echo round(0.95)."<br>";
echo round(2.49)."<br>";
echo round(2.50)."<br>";
echo round(2.51)."<br>";


?>

</body>
</html>
Output:
0
1
2
3
3

Here is another example of round() function in PHP:

<html>
<body>

<?php

echo round(1.11)."<br>";
echo round(-1.11)."<br>";
echo round(-0.21)."<br>";
echo round(-0.67)."<br>";

?>

</body>
</html>
Output:
1
-1
-0
-1