Round up to the nearest 50,000 in PHP

半城伤御伤魂 提交于 2019-12-01 12:39:25

问题


Is there a way to round up to the nearest 50,000 in PHP?

I've investigated round, but the docs don't suggest a way to do this, and it looks as though it is only for rounding up/down to the nearest numner. Thanks.


回答1:


/**
 * 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

Divide by the number you want to round against, do the rounding, then multiply by it again.




回答2:


How about this:

$number = ceil($value / 50000) * 50000;



回答3:


Try this:

$number = "78921";
$round = round($number / 50000) * 50000;


来源:https://stackoverflow.com/questions/12745332/round-up-to-the-nearest-50-000-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!