0

有没有办法在 PHP 中四舍五入到最接近的 50,000?

我已经调查过了,但是文档没有建议这样做的方法,而且看起来它似乎只是用于向上/向下舍入到最接近的数字。谢谢。

4

3 回答 3

11
/**
 * Round a number up to the nearest multiple of $n.
 *
 * @param int $int  Number to round.
 * @param int $n    Round to the nearest $n.
 *
 * @return int
 */
function round_up_to_nearest_n($int, $n) {
    return ceil($int / $n) * $n;
}
echo round_up_to_nearest_n(74268, 50000); //Outputs 100000

除以要舍入的数字,进行舍入,然后再乘以。

于 2012-10-05T11:32:33.143 回答
3

这个怎么样:

$number = ceil($value / 50000) * 50000;
于 2012-10-05T11:31:58.933 回答
0

尝试这个:

$number = "78921";
$round = round($number / 50000) * 50000;
于 2012-10-05T11:32:05.797 回答