How to round to nearest Thousand?

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

How Do I round a number to its nearest thousand?

function round($var) {
    // Round it
}
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 05:05

    Here's an answer to round up to the next thousand:

    abs(round(($a - 500), -3)) . "\n";

    500-999 then round to 0 and 1000-1999 round to 1000, etc.

    If you want it to start at 1000, then just do

    abs(round(($a + 500), -3)) . "\n";

    Cheers!

提交回复
热议问题