How to round to nearest number in php?
In this post, this function will round the number to nearest in php.
1. Code:
<?php
function roundToNearest($number){
$n = (int)$number;
$frac = $number - $n;
return ($frac >= 0.5) ? $n + 1 : $n;
}
$number = 8.5;
echo roundToNearest($number);
$number = 8.1;
echo roundToNearest($number);
?>
2. Example:
Input:
input number: 8.6 input number: 8.1
Output:
Output: 9 Output: 8