The round Function
The round
function rounds a number according to the rules
of mathematical rounding. The first parameter
accepts a number, and the second optional
parameter specifies how many digits to leave in the fractional
part. The second parameter can be either positive
or negative (in which case it specifies
how many digits to leave in the integer part).
Syntax
round(number, [precision]);
Example
Let's round a fraction to a whole number:
<?php
echo round(3.4);
?>
Code execution result:
3
Example
Let's round a fraction to a whole number:
<?php
echo round(3.5);
?>
Code execution result:
4
Example
Let's round a fraction to a whole number:
<?php
echo round(3.6);
?>
Code execution result:
4
Example
Let's round a number to two digits in the fractional part:
<?php
echo round(12.45678, 2);
?>
Code execution result:
12.46
Example
Let's round a number to three digits in the fractional part:
<?php
echo round(12.45678, 3);
?>
Code execution result:
12.457
Example
Let's round a number to tens:
<?php
echo round(12456.78, -1);
?>
Code execution result:
12460
Example
Let's round a number to hundreds:
<?php
echo round(12456.78, -2);
?>
Code execution result:
12500
Example
Let's round a number to thousands:
<?php
echo round(12456.78, -3);
?>
Code execution result:
12000