1

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
elixenide
  • 44,308
  • 16
  • 74
  • 100
Shiro
  • 7,344
  • 8
  • 46
  • 80

2 Answers2

14

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));
}

Here's a demo.

elixenide
  • 44,308
  • 16
  • 74
  • 100
6

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!

Rwd
  • 34,180
  • 6
  • 64
  • 78