Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round number up to `.49` or `.99`

Tags:

php

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 ?

like image 632
Jérôme Avatar asked Nov 18 '25 16:11

Jérôme


2 Answers

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);
like image 72
Elvin Haci Avatar answered Nov 21 '25 07:11

Elvin Haci


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

like image 43
Gumma Mocciaro Avatar answered Nov 21 '25 07:11

Gumma Mocciaro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!