Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round to nearest Thousand?

Tags:

php

How Do I round a number to its nearest thousand?

function round($var) {     // Round it } 
like image 527
Steven Hammons Avatar asked Mar 01 '11 02:03

Steven Hammons


People also ask

How do you round to the nearest 1000?

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.

What is 34519 rounded to the nearest thousands?

Therefore, the required answer will be 35000.

What does it mean to round a number to the nearest thousand?

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.


1 Answers

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:

  • add 500.
  • divide it by 1000 (and truncate to integer if necessary).
  • multiply by 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.

like image 63
paxdiablo Avatar answered Oct 21 '22 17:10

paxdiablo