Home  >  Article  >  Backend Development  >  How to Round Up to the Nearest Multiple of Five in PHP?

How to Round Up to the Nearest Multiple of Five in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-27 08:39:03366browse

How to Round Up to the Nearest Multiple of Five in PHP?

Rounding to the Nearest Multiple of Five in PHP

When working with numbers in PHP, it's often necessary to round them to the nearest specific value. One common scenario is rounding up to the nearest multiple of five.

Problem Statement

A PHP function is sought that takes an integer as input and returns its nearest multiple of five. For example, when called with 52, it should return 55.

The built-in round() function does not provide this functionality by default. When used with a negative precision, it rounds to the nearest power of ten.

Solution

To achieve the desired rounding behavior, a custom function can be created:

<code class="php">function roundUpToNearestMultiple($number, $multiplier = 5) {
    // Check if the number is already a multiple of the multiplier
    if ($number % $multiplier == 0) {
        return $number;
    }

    // Calculate the nearest multiple of the multiplier greater than the number
    $nextMultiple = ceil($number / $multiplier) * $multiplier;

    // Round the number up to the next multiple
    return $nextMultiple;
}</code>

Usage Example

<code class="php">echo roundUpToNearestMultiple(52); // Outputs 55
echo roundUpToNearestMultiple(55); // Outputs 55
echo roundUpToNearestMultiple(47); // Outputs 50</code>

Other Rounding Strategies

In addition to rounding up to the nearest multiple, you may encounter scenarios where different rounding strategies are required. Here are a few variations:

1. Round to the Next Multiple, Excluding the Current Number

<code class="php">function roundUpToNextMultiple($number, $multiplier = 5) {
    return roundUpToNearestMultiple($number + 1, $multiplier);
}</code>

2. Round to the Nearest Multiple, Including the Current Number

<code class="php">function roundToNearestMultipleInclusive($number, $multiplier = 5) {
    if ($number % $multiplier == 0) {
        return $number;
    }

    $lowerMultiple = floor($number / $multiplier) * $multiplier;
    $upperMultiple = ceil($number / $multiplier) * $multiplier;

    return round($number - $lowerMultiple) > round($upperMultiple - $number) ? $lowerMultiple : $upperMultiple;
}</code>

3. Round Up to an Integer, Then to the Nearest Multiple

<code class="php">function roundUpToIntegerAndNearestMultiple($number, $multiplier = 5) {
    $roundedNumber = ceil($number);

    if ($roundedNumber % $multiplier == 0) {
        return $roundedNumber;
    }

    return roundUpToNearestMultiple($roundedNumber, $multiplier);
}</code>

The above is the detailed content of How to Round Up to the Nearest Multiple of Five in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn