整数到最接近的千位
Round number up to nearest thousand
我需要将数字四舍五入到最接近的千位。我尝试了 round($x, -3)
,但输出并不是我想要的。
所需输出的示例:
999 => 1,000
1,000.0001 => 2,000
1,001 => 2,000
1,100 => 2,000
1,600 => 2,000
100,010 => 101,000
我不确定是否有特定功能可以满足您的需求,但您可以这样做:
(int) ceil($x / 1000) * 1000;
希望对您有所帮助!
您可以通过将 ceil()
与一些乘法和除法相结合来实现,如下所示:
function roundUpToNearestThousand($n)
{
return (int) (1000 * ceil($n / 1000));
}
更一般化:
function roundUpToNearestMultiple($n, $increment = 1000)
{
return (int) ($increment * ceil($n / $increment));
}
我需要将数字四舍五入到最接近的千位。我尝试了 round($x, -3)
,但输出并不是我想要的。
所需输出的示例:
999 => 1,000
1,000.0001 => 2,000
1,001 => 2,000
1,100 => 2,000
1,600 => 2,000
100,010 => 101,000
我不确定是否有特定功能可以满足您的需求,但您可以这样做:
(int) ceil($x / 1000) * 1000;
希望对您有所帮助!
您可以通过将 ceil()
与一些乘法和除法相结合来实现,如下所示:
function roundUpToNearestThousand($n)
{
return (int) (1000 * ceil($n / 1000));
}
更一般化:
function roundUpToNearestMultiple($n, $increment = 1000)
{
return (int) ($increment * ceil($n / $increment));
}