-
Rounding to Halves?
Can anyone think of a good way to do this? I'm struggling.
I'm trying to write a function that will round a number to an integer or .5 for example:
3.2342 would round to 3.0
3.3453 would round to 3.5
3.6345 would round to 3.5
3.7666 would round to 4.0
you know what I mean? :p
-
Re: Rounding to Halves?
This is the best way I can think of doing it.
PHP Code:
<?php
function round_half($num)
{
$frac = $num - ( (int) $num);
if ($frac >= 0.75) {
return ((int)$num) + 1;
} else if ($frac >= 0.25) {
return ((int)$num) + 0.5;
} else {
return (int) $num;
}
}
?>