The function in PHP for rounding is round(). The syntax format is as follows:
float round ( float val [, int precision] )
Returns the result of rounding val to the specified precision (the number of digits after the decimal point). precision can also be negative or zero (the default value).
Example
Round numbers to two decimal places, set negative numbers:
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
In addition to using the round() method, we can also use the sprintf() method to implement rounding:
<?php
$num=0.0215489;
echo sprintf("%.3f", $num); // 0.022
$num2 = 123213.066666;
echo sprintf("%.2f", $num2); // 123213.07
?>
Reference Article
- PHP round() Function
- PHP sprintf() Function