Round up to nearest multiple of five in PHP

Round up to nearest multiple of five in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Rounding to the Nearest Multiple of Five in PHP

The standard round() function is often the first instinct when dealing with rounding logic, but it operates on a specific axis: powers of ten relative to zero. When you call round(94, -1), PHP returns 90 because it rounds down to the nearest multiple of 10. This behavior fails immediately if your requirement is to round up or to the nearest available slot in a sequence like 5, 10, 15, 20, etc. The goal isn't just mathematical rounding; it's mapping any integer onto a specific grid.

To achieve a result where 52 becomes 55, you need to shift your perspective from the number itself to the distance between the target multiples. Since 5 is the base unit here, every multiple of five is exactly $n \times 5$. The strategy involves normalizing the input so that rounding to the nearest integer automatically maps it to the nearest multiple of five.

The most robust approach for integers is using a simple arithmetic formula that handles both positive and negative numbers correctly without relying on floating-point quirks. The logic follows these steps:

  1. Divide the number by 5.
  2. Add 0.5 to shift the rounding boundary.
  3. Use floor() to round down to the nearest whole integer.
  4. Multiply back by 5.

Here is the implementation:

function roundToNearestFive(int $number): int {
    return (int) floor($number / 5 + 0.5) * 5;
}

Testing this with 52 yields 55. Testing with 48 also yields 50, while 47 rounds down to 45. This method avoids the pitfalls of floating-point arithmetic by keeping the intermediate calculation in a predictable range before the final multiplication. If you strictly need to round up (ceiling) regardless of proximity, the formula changes slightly to use ceil() instead of floor().

For developers working within the Laravel ecosystem, there is no built-in helper specifically for this niche rounding requirement, so wrapping this logic in a custom service or utility class is a common pattern. You might place this function in an App\Utilities\NumberFormatter class to keep your controllers clean. This keeps your core business logic decoupled from specific mathematical edge cases.

However, if you are dealing with currency or values where precision is critical, be wary of floating-point representation issues. While the formula above uses integers effectively, dividing by 5 can technically introduce float behavior if the input were a float string like "52.0". In such cases, casting the input to an integer first ensures consistent behavior:

function roundToNearestFiveSafe(float|int $number): int {
    return (int) floor((float)$number / 5 + 0.5) * 5;
}

It is also worth noting that this approach does not handle rounding to the nearest multiple of five for negative numbers differently than positive ones if you simply want "nearest." The floor() function combined with the + 0.5 offset works symmetrically around zero. For instance, -52 divided by 5 is -10.4. Adding 0.5 gives -9.9. Flooring that results in -10, which multiplied by 5 returns -50. This correctly identifies -50 as the nearest multiple of five to -52.

If your application requires strict adherence to "round half up" rules where .5 always goes away from zero, this formula satisfies that requirement. However, if your domain logic dictates that any fraction pushes the value up (always round up), you would swap floor for ceil. The trade-off here is between mathematical neutrality (nearest) and business rule compliance (always up). Always verify which direction ties in your specific use case before deploying.

For more complex scenarios involving ranges or buckets, this function provides a solid foundation. If you need to handle large datasets where performance is paramount, the arithmetic operations are extremely fast compared to using regular expressions or string manipulation hacks often found in less robust solutions.

Our network