How Do I round a number to its nearest thousand?
function round($var) { // Round it }
To round a number to the nearest 1000, look at the hundreds digit. If the hundreds digit is 5 or more, round up. If the hundreds digit is 4 or less, round down. The hundreds digit in 4559 is 5.
Therefore, the required answer will be 35000.
Rounding of a number means replacing it with another number that is nearly equal to it but easy to represent or write. For example 754 rounded to nearest thousand is 1000. If the rounding number is 4 or less it is round down. If the rounding number is 5 or more it is rounded up.
PHP allows negative precision for round
such as with:
$x = round ($x, -3); // Uses default mode of PHP_ROUND_HALF_UP.
Whereas a positive precision indicates where to round after the decimal point, negative precisions provide the same power before the decimal point. So:
n round(1111.1111,n) == ================== 3 1111.111 2 1111.11 1 1111.1 0 1111 -1 1110 -2 1100 -3 1000
As a general solution, even for languages that don't have it built in, you simply do something like:
500
.1000
(and truncate to integer if necessary).1000
.This is, of course, assuming you want the PHP_ROUND_HALF_UP
behaviour. There are some who think that bankers rounding, PHP_ROUND_HALF_EVEN
, is better for reducing cumulative errors but that's a topic for a different question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With