Rounding A Number To Nearest The Thousand In PHP

I have previously talked about rounding numbers in PHP, but what if you wanted to round the number to the nearest thousand?

It is possible to do this with the native round() function in PHP by using a negative number as the second parameter. The round() function has two parameters, the first is the number to be rounded and the second is the number of places to round the number to, also known as the precision. The default for round() is to round the number to the nearest whole number, by using positive numbers as the second parameter you can set the number of decimal places to round the number to. Giving a negative number as the second parameter will allow you to round the number to the nearest full number. For example, to round a number to the nearest thousand you can put -3 as the precision, this will change the number 12,345 into 12000.

Here is a full example:

$num = 123456789;

echo round($num,0); // whole number - prints 123456789
echo round($num,-1); // ten - prints 123456790
echo round($num,-2); // hundred - prints 123456800
echo round($num,-3); // thousand - prints 123457000
echo round($num,-4); // ten thousand - prints 123460000
echo round($num,-5); // hundered thousand - prints 123500000
echo round($num,-6); // million - prints 123000000
echo round($num,-7); // ten million - prints 120000000
echo round($num,-8); // hundred million - prints 100000000
echo round($num,-9); // billion - prints 0

Note that if you set the precision to be more than the actual number present then you with either get the next highest number or zero. For example, when rounding the number 15, if the precision is -1 then the result is 20 as this is the nearest full ten. However, if -2 is used then the result is 0 as this is the nearest full hundred.

This can cause errors if you are rounding lots of different sized values, so a neat little trick is to use the strlen() function to find out how many characters are in a number and then round to the nearest largest number. You can take the number of characters, subtract 1 to make it one less than the length of the string or you will round some numbers down to zero. You then multiply this value by -1 to invert it.

$num = 151;
$round = (strlen($num)-1)*-1;
echo round($num,$round); // prints 200

Comments

I am trying to round $705,666.66 to the nearest whole thousand. Is $706,000 correct?

 

 

Permalink

thank you soo much for the.. it was a great help. :) :) :)

Permalink
I found that not casting the $num variable as an integer before passing it to strlen() caused strlen to count length wrong if $num is a float, thus breaking your code. Here's a function I wrote around your code:#Function to get original amount after percent increase or decrease function getOriginalAmountAfterPercentChange($currentNum, $percentChange){ $number = ($currentNum / ($percentChange + 1) * 100); #Get current number $precision = ((strlen((int)$number) -3) * -1); #Figure out what precision to round to return round($number, $precision); }
Permalink
Good point! Thanks for the function correcting that :)
Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.