Reputation: 2056
I'm trying to round number like this :
5,24785 => 5,49
5,74536 => 5,99
5,00000 => 5,00
I tried with https://php.net/round
But this return
5,24785 => 5,24
5,74536 => 5,74
5,00000 => 5,00
A php function can do it or I need to round up then remove 0.01 ?
Do you have some clues ?
Upvotes: 3
Views: 2311
Reputation: 3572
You need using custom round function for this. Because rounding to 0.49 is not a standard way.
function rounder($num){
$fln = $num-floor($num);
if ($fln>0 and $fln<0.5) $fln=0.49;
else $fln=0.99;
return floor($num)+$fln;
}
echo rounder(5.24882);
Upvotes: 3
Reputation: 1227
I modified @GummaMocciaro solution, which did the job for me. But I extended this code slightly, because the case x.5
was not handled and null was returned.
I transformed it into else-if
statements and assigned $myNumber = $number
in the else
block, which is the case for x.0
and x.5
, so a valid number is always returned.
function roundPrice($number) {
$decimal = $number - (int)$number;
$myNumber = $number;
// x.49
if ($decimal > 0 && $decimal < 0.5) {
$myNumber = (int)$number + 0.49;
}
// x.99
else if ($decimal > 0.5) {
$myNumber = (int)$number + 0.99;
}
// else x.0 or x.5
return $myNumber;
}
Upvotes: 1
Reputation: 31
$num = 5.56;
$num = round($num - (int) $num) ? round($num) - 0.01 : round($num) + .49;
// 5.99 output
$num = 5.38;
$num = round($num - (int) $num) ? round($num) - 0.01 : round($num) + .49;
// 5.49 output
Upvotes: 0
Reputation: 1215
There is no such a thing in php (i believe), you can do a manual check:
$number = "5.85458";
$decimal = $number - (int) $number;
if($decimal > 0 && $decimal < 0.5) $myNumber = (int) $number + 0.49; // x.49
if($decimal > 0.5) $myNumber = (int) $number + 0.99; // x.99
if($decimal == 0) $myNumber = (int) $number; // x.00
echo $myNumber;
Or just remove 0.01
Upvotes: 2
Reputation: 16063
there is no function can do it, you need to round up then remove 0.01
Upvotes: 1