How to round half up numbers in php?
In this post, this function will round the number to half a unit in php.
1. Code:
<?php function roundHalfUp($number){ $n = (int)$number; $frac = $number - $n; if ($frac > 0 && $frac < 0.5) { $number = $n + 0.5; } else if ($frac > 0.5) { $number = $n + 1; } return $number; } $number = 8.1; echo roundHalfUp($number); $number = 8.6; echo roundHalfUp($number); ?>
2. Example:
Input:
Number input: 8.1 Number input: 8.6
Output:
Output: 8.5 Output: 9