Skip to Content
Course content

106: Basic Math Functions: round, ceil, floor

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was working on a shipping module for a client yesterday and ran into a classic problem: decimals. Specifically, I had a package weight of 4.3 kilograms, but the courier's API requires whole numbers for certain billing tiers. I can't just cast it to an (int) because that just chops the decimal off, and depending on the business rule, that might actually be the wrong move.

The problem with simple casting

First, I tried the quickest way to get rid of the decimal. I just cast the float to an integer. Let's see what happens here:

$weight = 4.7;
echo (int)$weight; // Output: 4

Wait. If the package is 4.7kg, and I tell the courier it's 4kg, I'm essentially stealing 0.7kg of capacity. My client will lose money. Casting to an int effectively always rounds down, regardless of how close we are to the next whole number. I need something more intentional.

Trying to be "fair" with round()

Most of the time, when we think of rounding, we think of the standard math class rule: .5 and up goes up, everything else goes down. PHP has a built-in for this: round().

$weight = 4.3;
echo round($weight); // Output: 4

$weight = 4.6;
echo round($weight); // Output: 5

That works for a general average, but in logistics, "fair" isn't usually how billing works. If a package is 4.1kg, the courier is still going to charge me for a 5kg slot because it doesn't fit in the 4kg box. round() is too lenient here. I need a way to force the number up, no matter how small the decimal is.

Forcing the ceiling

This is where ceil() comes in. I remember the term "ceiling"β€”it's the top of the room. It pushes any decimal, no matter how tiny, up to the next highest integer.

$weight = 4.1;
echo ceil($weight); // Output: 5

$weight = 4.0;
echo ceil($weight); // Output: 4

Perfect. If there is even a fraction of a gram over 4, ceil() bumps it to 5. This is exactly what the courier wants. It's a "safe" way to ensure we never under-calculate the space or cost required.

Ignoring the leftovers with floor()

Now, let's flip the script. Suppose I'm calculating how many full 1kg weights I can fit into a shipping crate. If I have 4.9kg of material, I only have 4 full kilograms; that last 0.9 doesn't count as a complete unit.

I could use (int) casting like I did at the start, but in my experience, using floor() makes your intent much clearer to the next developer reading your code. It says, "I am intentionally rounding down to the floor."

$material = 4.9;
echo floor($material); // Output: 4

It's the exact opposite of ceil(). While round() is a gamble based on the .5 threshold, ceil() and floor() are absolute. One always goes up, one always goes down.

A quick note on precision

Before I move on, I should mention that round() is the only one of these three that lets you specify precision. If I didn't want a whole number, but maybe just two decimal places for a price, I can do this:

$price = 12.5678;
echo round($price, 2); // Output: 12.57

ceil() and floor() don't do that; they always return a float that represents a whole integer. If you need to "ceil" to two decimal places, you'll have to get a bit more creative with multiplication and division, but for basic whole-number logic, these three are your bread and butter.




πŸ“‹ Practical Task

Build a Warehouse Shelf Capacity Calculator

You are writing a script to help a warehouse manager organize items on a shelf. Create a PHP script that does the following:

  • Define a variable $totalItemWidth with a decimal value (e.g., 15.3).
  • Define a variable $shelfSlotWidth with a value of 5.
  • Calculate how many full slots are completely filled by the items using floor().
  • Calculate the total number of slots that will be occupied (even if the last slot is only partially filled) using ceil().
  • Print both results with descriptive labels.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.