tags:

views:

120

answers:

3

Hi,

the function of ceil() in php is not useful in my case, because i want if the number contains from 0.50 to 0.99 then it's will ceil it, if it's smaller than 0.50 it's will not do anything.

example:

6523.70 will be 6524

but

6523.49 will stay same.

i hope you got it guys :)

Thanks

+1  A: 

http://php.net/round

Frank Farmer
Thanks frank so much!
David
According to the question, 6523.49 must stay as 6523.49. Round will round this down.
taspeotis
I don't follow - could someone elaborate on how PHP's round() handles the requirements in the question (even if they are rather unusual requirements)?
Michael Burr
It doesn't, unless the user meant "6523.49 will stay same integer component"
taspeotis
I'm guessing, since the OP accepted this answer, that they meant 6523.49 should round down rather than stay as 6523.49 :-) @Frank, I'm not a big fan of link-only answers (I don't mind links but I prefer answers that are useful even when the rest of the internet disappears). If you'd like to flesh out your answer somewhat, I'll give you an upvote.
paxdiablo
+4  A: 

Forgive me, I haven't written PHP in a while.

if( ( $var - intval( $var ) ) >= 0.5 ) $var = ceil( $var )

Thus,

6523.70 to 6524 and 6523.49 stays 6523.49

taspeotis
That looks right, but I think you mean intval() (there is no int() function).
Alex JL
What int() function?;-)
taspeotis
+9  A: 

This will only change numbers which are >= .5

function weirdRounding($num) {
    if ($num - floor($num) >= .5) {
        return ceil($num);
    }
    return $num;
}

weirdRounding(6523.70) --> 6524
weirdRounding(6523.49) --> 6523.49
nickf