Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Mathematics Wish List | Report Bug | About Me ]

4.6.1     Rounding: ceil(), floor(), and round()

This is NOT the latest copy of this book; click here for the latest version.

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:

<?php
    $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 the same way as typecasting, except typecasting is faster.

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.

<?php
    $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.





<< 4.6 Mathematics   4.6.2 Randomisation: rand(), mt_rand(), getrandmax(), mt_getrandmax(), srand(), and mt_srand() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 05 Dec 2008

To A PHP User:
You could probably divide by 1000, use floor, then multiply by 1000.

cast3r - 05 Dec 2008

its also kinda solution ;] :
your_num -= your_num%1000;

Unix Programmer - 05 Dec 2008

In answer to the question, using php5, the following works:

print round(1628335, -3) . "\n"
print round(1628635, -3) . "\n"

prints:

1628000
1629000

A PHP User - 05 Dec 2008

Need to know how to round down numbers as follows:

1,628,335 (result must be 1,628,000)
1,628,635 (result must be 1,628,000)

Appreciate if an expert will email solution to gbpurgatorio@yahoo.com.

Thanks in advance.

Kyku <kyku at os dot pl> - 05 Dec 2008

"Author's Note: The floor() function converts a floating-point number to an integer in the same way as typecasting, except typecasting is faster."

This is not true for negative numbers, e.g.
floor(-3.5) -> -4
(int) -3.5 -> -3



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow