How Do I round a number to its nearest thousand?
function round($var) {
// Round it
}
Just a small change, may help to round up!
$x = ceil(220.20 / 1000) * 1000;
echo $x;
rounded_number = round(original_number, -3);
http://php.net/manual/en/function.round.php
Use the round function as mentioned by other posters round($number, -3)
.
You can also, divide your number by 1,000, round to nearest whole number then multiply by 1,000.
Also, if you want to round up, you can divide by 1,000, negate the quotient, coerce it to an integer, negate it again and then multiply it by 1,000.
from: http://us3.php.net/manual/en/function.round.php
$x = round ( $x, -3 );
For positive integers:
function round($var) {
return ($var + 500) / 1000 * 1000;
}
Just for your information simple calculation from Mikel answer is faster than round():
Test name Repeats Result Performance
calculation 10000 0.030229 sec +0.00%
round 10000 0.040981 sec -35.57%
Test source here.