.


Divya

PHP round() function with real time example


The PHP round() function rounds a floating number.

It Returns the rounded value of number to specified precisions.

Syntax

round(number,precision,mode);

Example 1

<!DOCTYPE html>
<html>
<body>
<?php
echo(round(0.75) . "<br>");
echo(round(1.20) . "<br>");
?>
</body>
</html>

output

1
1

Example 2

<!DOCTYPE html>
<html>
<body>
<?php
echo(round(4.96754,2) . "<br>");
echo(round(7.045,2) . "<br>");
echo(round(7.055,2));
?>
</body>
</html>

Output

4.97
7.05
7.06

Example 3

<!DOCTYPE html>
<html>
<body>
<?php
echo(round(2.5,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(2.5,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(2.5,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(2.5,0,PHP_ROUND_HALF_ODD) . "<br>");
?>
</body>
</html>

Output

3
2
3
2



.