Reputation: 317
$temp
is currently 6. But the variable result can be changing every time to a different number so it is not a fixed value.
Anyway, for this $temp * 1.1666666
, the result will be 6.99999996
. Since I used the floor function, it will be rounded down to 6.
Is there any way when the value is more then>*.49999
it will stay at *.5
instead of *
?
Example: 6.51111111
, 6.78948123
, 6.9747124
Expected Output: 6.5
Example: 6.49999999
, 6.12412431
, 6.33452361
Expected Output: 6
Do note that, $temp value will be ever changing..thank you!
Upvotes: 1
Views: 136
Reputation: 4858
Try this code...
<?php
$temp = 6.94444;
echo myRound($temp);
function myRound($temp)
{
$frac = $temp - floor($temp);
$frac = ($frac >= .5) ? .5 : 0;
return ( floor($temp) + $frac );
}
?>
Hope this is what you want.
Upvotes: 0
Reputation: 219834
Use round($number, 1)
. That will round to the nearest decimal point.
$number = round(.1666666 * $temp, 1);
If you want to round to the nearest half you can do this:
function round_to_half($num)
{
if($num >= ($half = ($ceil = ceil($num))- 0.5) + 0.25) return $ceil;
else if($num < $half - 0.25) return floor($num);
else return $half;
}
$number = round_to_half(.1666666 * $temp);
Upvotes: 5