I need round a number up to the nearest thousand. I tried round($x, -3)
, but the output is not really what I want.
Examples of the desired output:
999 => 1,000
1,000.0001 => 2,000
1,001 => 2,000
1,100 => 2,000
1,600 => 2,000
100,010 => 101,000
I need round a number up to the nearest thousand. I tried round($x, -3)
, but the output is not really what I want.
Examples of the desired output:
999 => 1,000
1,000.0001 => 2,000
1,001 => 2,000
1,100 => 2,000
1,600 => 2,000
100,010 => 101,000
You can do this by combining ceil()
with some multiplication and division, like this:
function roundUpToNearestThousand($n)
{
return (int) (1000 * ceil($n / 1000));
}
More generically:
function roundUpToNearestMultiple($n, $increment = 1000)
{
return (int) ($increment * ceil($n / $increment));
}
I'm not sure if there is a specific function for what you're after but you could do something like:
(int) ceil($x / 1000) * 1000;
Hope this helps!