A lot of programs, both big and small, require us to deal with numbers. There are two ways of defining numbers in PHP. They can either be floats or integers. We can also use strings to represent very large numbers. You can learn more about these data types and converting between them in this tutorial on integers, floats, and number strings in PHP.
Here, our focus will be on some functions related to integers and floats. We will be discussing floor()
, ceil()
, and round()
in this tutorial.
Definitions
Let’s start with the purpose of all the functions.
-
floor()
: This function rounds floats down to the preceding whole number. For example, 5.2 becomes 5, and 8.9 becomes 8. -
ceil()
: This function rounds floats up to whole numbers. For example, 5.2 becomes 6, and 8.9 becomes 9. -
round()
: This function returns a float that has been rounded up or down and approximated to the specified precision. If you don’t specify any precision, it will round to the nearest whole number: 5.2 becomes 5, and 8.9 becomes 9.
Remember that the return value of these functions will be integers mathematically speaking, but they would still be a floating point data type.
Which One Should You Use?
All the functions are useful in specific situations that we will cover below:
PHP floor()
vs. PHP ceil()
The floor()
and ceil()
functions are useful when you’re dealing with situations which only require the use of whole numbers. Consider the following two examples:
Say you have to write a program which determines the number of containers that would be needed to ship some eggs to market. We are bound by two restrictions here. First, containers cannot be fractions. Second, we need to ship all the eggs. This means that we will need a whole container, even if it is not filled up entirely. So we should use ceil()
.
<?php $egg_count = 510; $container_capacity = 50; $containers_needed = ceil($egg_count/$container_capacity); echo $containers_needed; // 11 ?>
Consider another situation where you have to determine how many people can be fully fed with the available food. In this case, you only have to invite people if you are certain they can be fed with the available food without ordering anything extra. We will have to ignore the leftover food which cannot feed an adult. So we should use floor()
.
<?php $burger_count = 110; $burgers_per_person = 3; $persons_fed = floor($burger_count/$burgers_per_person); echo $persons_fed; // 36 ?>
PHP round()
The PHP round()
function is different from floor()
and ceil()
because it takes us to the nearest integer (when no precision is specified) instead of always rounding up or down. It takes three parameters:
- The number you want to round.
- The precision for rounding the number. The default value is zero, which means no digits after the decimal.
- The mode for rounding numbers that are halfway, like 3.5, 2.5, and -4.5.
This function is usually used to simplify numbers in everyday situations where we don’t need full precision. Scientifically, we can also use round()
to get rid of additional spurious precision added by calculations over the precision of data used in those calculations.
<?php $people_count = 13097824; $car_count = 3987; function make_approximations($number, $digits) { if ($digits < 0) { return round($number, $digits)/10**abs($digits); } else { return round($number, $digits); } } echo 'There are approximately '.make_approximations($people_count, -6).' million people in our city.'; // There are approximately 13 million people in our city. echo 'Around '.make_approximations($car_count, -3).' thousand cars pass this intersection everyday.'; // Around 4 thousand cars pass this intersection everyday. $plot_length = 22.03; $plot_width = 20.18; echo 'Our plot is around '.make_approximations($plot_length*$plot_width, 2).' sq.m. in area.'; // Our plot is around 444.57 sq.m. in area. ?>
In short, for everyday life, using round()
is helpful in situations where the exact value of a number is not important and we need an approximation that’s easy to remember.
Final Thoughts
In this tutorial, we have briefly discussed how to use floor()
, ceil()
, and round()
. The floor()
function is useful when you always want to get the next lowest integer, and ceil()
is useful when you want to get the next highest integer. The round()
function is used to round to the closest number (integer or otherwise) with the specified precision.