Hacking with PHP has been updated for PHP 7 - click here! >>


float ceil ( float value)

float floor ( float value)

float round ( float value [, int precision])

It is a common situation that you want less accuracy than PHP gives you, in which case you need to use one of PHP's selection of rounding functions: ceil(), floor(), and round().

Both ceil() and floor() take just one parameter - the number to round. Ceil() takes the number and rounds it to the nearest integer above its current value, whereas floor() rounds it to the nearest integer below its current value. Here is an example:

    $someval = 4.9;
    $ceiled = ceil($someval);
    $floored = floor($someval);

After executing that code, $ceiled will be 5 and $floored will be 4.

Author's Note: The floor() function converts a floating-point number to an integer in roughly the same way as typecasting, except typecasting is faster. The only difference is with negative numbers, where floor() will round -4.5 down to -5 whereas typecasting would return -4.

The other function available is round(), which takes two parameters - the number to round, and the number of decimal places to round to. If a number is exactly half way between two integers, round() will always round up.

    $a = round(4.9); // 5
    $b = round(4.5); // 5
    $c = round(4.4999); // 4
    $d = round(4.123456, 3); // 4.123
    $e = round(4.12345, 4); // 4.1235
    $f = round(1000 / 160); // 6

As you can see, 4.5 is rounded up to 5, whereas 4.4999 is rounded down to 4. Line four has parameter two being used for the first time, which shows that it is very easy to round to a given number of decimal places. Note that in line five we get 4.1235 when rounding to four decimal places, because PHP looks one digit further to decide on the last digit. That is, if our number were 4.12344, rounding to four decimal places would give us 4.1234, but because the fifth digit is a 5, which is always rounded up, we find ourselves with 4.1235 instead.

The last example, $f, is a common situation encountered by people using round(). Imagine you were organising a big trip to the countryside, and 1000 people signed up. You need to figure out how many coaches you need to hire, so you take the number of people, 1000, and divide it by the capacity of your coaches, 160, then round it to get a whole number, and you find the result is 6.

Where is the problem? Well, the actual result of 1000 / 160 is 6.25 - you need six and a quarter coaches to transport 1000 people, and you will only have ordered six because round() rounded towards 6 rather than 7 because it was closer. As you cannot order six and a half coaches, what do you do? The solution is simple: in situations like this, you use ceil(). All three rounding functions are useful in their own right, so be sure to try to remember them all.


If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Randomisation >>

Previous chapter: Mathematics

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.