PHP相当于Excel的MROUND函数,舍入到最接近的倍数

时间:2022-07-15 02:56:41

Excel has the MROUND function which rounds a number up/down to a given multiple.

Excel具有MROUND功能,可将数字向上/向下舍入到给定倍数。

=MROUND(600, 400) //--> 800
=MROUND(14,4)     //--> 16
=MROUND(0.5,2)    //--> 0

What is the equivalent function for PHP?

PHP的等价函数是什么?

If there is none, how would you do it?

如果没有,你会怎么做?

3 个解决方案

#1


1  

You can achieve the same effect by dividing by the denominator, rounding it, then multiplying it again by the denominator. Eg:

您可以通过除以分母,四舍五入,然后再乘以分母来实现相同的效果。例如:

function roundTo($number, $to)
{
    return round($number/$to, 0)* $to;
}

echo roundTo(87.23, 20); //80

echo roundTo(600, 400) // 800
echo roundTo(14,4)     // 16
echo roundTo(0.5,2)    // 0

#2


4  

The PHPExcel implementation of MROUND()

MROUND()的PHPExcel实现

function MROUND($number,$multiple) {
    if ((is_numeric($number)) && (is_numeric($multiple))) {
        if ($multiple == 0) {
            return 0;
        }
        if ((SIGNTest($number)) == (SIGNTest($multiple))) {
            $multiplier = 1 / $multiple;
            return round($number * $multiplier) / $multiplier;
        }
        return 'NAN';
    }
    return 'NAN';
}   //  function MROUND()

function SIGNTest($number) {
    if (is_bool($number))
        return (int) $number;
    if (is_numeric($number)) {
        if ($number == 0.0) {
            return 0;
        }
        return $number / abs($number);
    }
    return 'NAN';
}   //  function SIGN()

#3


1  

The basic logic is this:

基本逻辑是这样的:

$number= 600;
$unit= 400;
$remainder= $number % unit;
$mround = ($remainder < $unit/2) ? $number - $remainder : $number + ($unit-$remainder);

You'll need to validate your inputs to be sure they are numbers and to avoid dividing by zero.

您需要验证输入以确保它们是数字并避免除以零。

#1


1  

You can achieve the same effect by dividing by the denominator, rounding it, then multiplying it again by the denominator. Eg:

您可以通过除以分母,四舍五入,然后再乘以分母来实现相同的效果。例如:

function roundTo($number, $to)
{
    return round($number/$to, 0)* $to;
}

echo roundTo(87.23, 20); //80

echo roundTo(600, 400) // 800
echo roundTo(14,4)     // 16
echo roundTo(0.5,2)    // 0

#2


4  

The PHPExcel implementation of MROUND()

MROUND()的PHPExcel实现

function MROUND($number,$multiple) {
    if ((is_numeric($number)) && (is_numeric($multiple))) {
        if ($multiple == 0) {
            return 0;
        }
        if ((SIGNTest($number)) == (SIGNTest($multiple))) {
            $multiplier = 1 / $multiple;
            return round($number * $multiplier) / $multiplier;
        }
        return 'NAN';
    }
    return 'NAN';
}   //  function MROUND()

function SIGNTest($number) {
    if (is_bool($number))
        return (int) $number;
    if (is_numeric($number)) {
        if ($number == 0.0) {
            return 0;
        }
        return $number / abs($number);
    }
    return 'NAN';
}   //  function SIGN()

#3


1  

The basic logic is this:

基本逻辑是这样的:

$number= 600;
$unit= 400;
$remainder= $number % unit;
$mround = ($remainder < $unit/2) ? $number - $remainder : $number + ($unit-$remainder);

You'll need to validate your inputs to be sure they are numbers and to avoid dividing by zero.

您需要验证输入以确保它们是数字并避免除以零。