Search code examples
phpfloorceil

How to floor/ceil number to get this format using php?


How to floor/ceil number to get this format using php ?

I want to get like this result

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 3.0
3.2 =======> 3.0
3.3 =======> 3.0
3.4 =======> 3.0
3.5 =======> 4.0
3.6 =======> 4.0
3.7 =======> 4.0
3.8 =======> 4.0
3.9 =======> 4.0

...................................................................

So, i use this code

$x = floor($input);

But i get this result

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 3.0
3.2 =======> 3.0
3.3 =======> 3.0
3.4 =======> 3.0
3.5 =======> 3.0
3.6 =======> 3.0
3.7 =======> 3.0
3.8 =======> 3.0
3.9 =======> 3.0

...................................................................

Then i use this code

$x = ceil($input);

But i get this result

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 4.0
3.2 =======> 4.0
3.3 =======> 4.0
3.4 =======> 4.0
3.5 =======> 4.0
3.6 =======> 4.0
3.7 =======> 4.0
3.8 =======> 4.0
3.9 =======> 4.0

How can i do for get thisa result ?

inpput====> output
.....
3.0 =======> 3.0
3.1 =======> 3.0
3.2 =======> 3.0
3.3 =======> 3.0
3.4 =======> 3.0
3.5 =======> 4.0
3.6 =======> 4.0
3.7 =======> 4.0
3.8 =======> 4.0
3.9 =======> 4.0

Solution

  • use round function. ex,

    $x = round ($input);
    

    Ref: http://php.net/manual/en/function.round.php