Error with CEIL function

Hello,

I am using the CEIL function to round up numbers, if needed. But when I encounter a result that doesn’t need to be rounded up, it will round up anyway:

$var = “5”;
echo “ceil: “.ceil($var).”
”; // result is 5
$var = 294.85 / 58.97; // value comes to 5
echo “var: $var
”;
echo “ceil: “.ceil($var).”
”; // the ceil value is 6 …

Thanks for any ideas on this.

Mike

Hi,

The reason you are getting different output when you believe you should be getting the same is that you are actually using two different types of variables.

ceil() takes a float as input and outputs an integer, however, if the input is an integer, it does not need to do any operation and simply returns what you passed into it.

Therefore:

[php]
$var = “5”; //this is an INT
echo “ceil: “.ceil($var).”
”; // result is 5, because ceil() was passed an INT, and did nothing
$var = 294.85 / 58.97; // value comes to 5, but because of the decimal points, even though no decimal places are shown, this $var is no longer an INT
echo “var: $var
”; // echoes 5, because the value of $var is 5
echo “ceil: “.ceil($var).”
”; // the ceil value is 6 … because it was passed 5 as a float and ran it’s normal operation of rounding up
[/php]

I believe you’re better off using round, and if you only want to round up and never down try using something like this:

[php]
echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10 - tells round to round 9.5, 0 decimal places, and if the value is half or higher, round up, otherwise, round down
[/php]

Hope this helps!

Robert

Whoops, that code will round the way we’re used to classically, sorry I misspoke.

Thanks Robert,

But if the value is 5.00, why does the ceil command round it up? I thought it only rounded up if it needed too?

Thank again,

Mike

Hi Mike,

The ceil() function will round up period, the only reason it does not round up the initial $var = “5”; is because as I said, that 5 is already an integer, and since it outputs an integer, it skips the rounding process of the function entirely.

If you pass ceil 5.00, it will round up to 6, if you pass it 0.1, it will round up to 1, if you pass it -0.1, it will result with -0 (yes, negative zero), if you say ceil(-0.1) + 0, it will then return 0.

Ceil is designed to round up, period, thus it’s name, which is short for ceiling. It’s brother function floor, always rounds down.

The only case in which ceil will not round up is if it is passed an integer.

Hope this helps!

Robert

One more thing, the value is not 5.00, if it was, you would see the .00 when you echo the $var after performing the division. The value is indeed 5, however, 5 as another number type (such as float), and not 5 as an integer.

The key difference to be aware of here is the type of variable, and how the operations leading up to the ceil() function will affect that variable type.

Robert

Thanks for you expaination.

I can understand how it does it, just dont’ understand why… :frowning: I thought the purpose of ceil was to always round up if there is any remainder, ie decimals values, so, two numbers that divide out to int number, shouldn’t be rounded up.

Upon further reveiew, I took that returned value, and did number_format out to 18 places, sure enough there are values out there starting at digit , 5.000000000000000888.

I got around this by formating my number to 14 decimals before doing the CEIL. This works for me, by acurately rounding to 5.

Thanks again for your responses!

Mike

Hi Mike,

That is some good homework you’ve done there.

Leave it to a programming language to foul up simple division, the quoted division equals 5 with no remainder, but PHP shows a remainder, even if it is way down the line of decimal places.

I’m no ceil() expert, I try to stay away from that function to be honest, and upon reading your latest post, I believe that perhaps it won’t round up if it receives 5.00, more than likely, it was the 8’s at the end of the value that were causing the problem.

Looks like we both learned something, ha!

Good luck and no problem for the responses!

Robert

Sponsor our Newsletter | Privacy Policy | Terms of Service