How to round to nearest Thousand?

前端 未结 8 592
独厮守ぢ
独厮守ぢ 2020-12-06 03:59

How Do I round a number to its nearest thousand?

function round($var) {
    // Round it
}
相关标签:
8条回答
  • 2020-12-06 04:39

    Just a small change, may help to round up!

    $x = ceil(220.20 / 1000) * 1000;
    echo $x;
    
    0 讨论(0)
  • 2020-12-06 04:41
    rounded_number = round(original_number, -3);
    

    http://php.net/manual/en/function.round.php

    0 讨论(0)
  • 2020-12-06 04:41

    Use the round function as mentioned by other posters round($number, -3).

    You can also, divide your number by 1,000, round to nearest whole number then multiply by 1,000.

    Also, if you want to round up, you can divide by 1,000, negate the quotient, coerce it to an integer, negate it again and then multiply it by 1,000.

    0 讨论(0)
  • 2020-12-06 04:49

    from: http://us3.php.net/manual/en/function.round.php

    $x = round ( $x, -3 );
    
    0 讨论(0)
  • 2020-12-06 04:56

    For positive integers:

    function round($var) {
        return ($var + 500) / 1000 * 1000;
    }
    
    0 讨论(0)
  • 2020-12-06 05:02

    Just for your information simple calculation from Mikel answer is faster than round():

    Test name       Repeats         Result          Performance     
    calculation     10000           0.030229 sec    +0.00%
    round           10000           0.040981 sec    -35.57%
    

    Test source here.

    0 讨论(0)
提交回复
热议问题