Round up to the nearest 50,000 in PHP

前端 未结 3 1409
青春惊慌失措
青春惊慌失措 2021-01-17 00:50

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 fo

相关标签:
3条回答
  • 2021-01-17 01:12
    /**
     * 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.

    0 讨论(0)
  • 2021-01-17 01:21

    Try this:

    $number = "78921";
    $round = round($number / 50000) * 50000;
    
    0 讨论(0)
  • 2021-01-17 01:26

    How about this:

    $number = ceil($value / 50000) * 50000;
    
    0 讨论(0)
提交回复
热议问题