Round number to nearest thousand, up or down depending on the number
This will do what you want:
Math.round(value/1000)*1000
examples:
Math.round(1001/1000)*1000
1000
Math.round(1004/1000)*1000
1000
Math.round(1500/1000)*1000
2000
Round a number to nearest thousand
First divide your number by 1000, then round, and multiply again:
var num = 89250;
var rounded = Math.round(num / 1000) * 1000;
If you want a different tie-breaking -- rounding ties down instead of up -- then apply the negation operator to the number before and after the rounding. Compare the difference in the output:
var num = 89500;var rounded = -Math.round(-num / 1000) * 1000;console.log('rounding tie down: ', rounded); // 89000
var num = 89500;var rounded = Math.round(num / 1000) * 1000;console.log('rounding tie up: ', rounded); // 90000
Round integer to nearest thousand
int number = 73400
return round(number/1000.0)*1000
Round number up to nearest thousand
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.
How to round to nearest Thousand?
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.
How to round up number to nearest 100/1000 depending on number, in JavaScript?
You could take the logarithm of ten and round up the value for getting the value.
function roundup(v) { return Math.pow(10, Math.ceil(Math.log10(v)));}
console.log(roundup(87)); // 100console.log(roundup(776)); // 1000console.log(roundup(2333)); // 10000
Round to nearest 1000 in pandas
By using the notation df.ColumnName.round()
, you are actually calling pandas.Series.round
, the documentation of which specifies:
decimals : int
Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.
So you can do:
df = pd.DataFrame({'val':[1,11,130,670]})
df.val.round(decimals=-2)
This produces the output:
0 0
1 0
2 100
3 700
Name: val, dtype: int64
decimals=-3
rounds to the 1000s, and so on. Notably, it also works using pandas.DataFrame.round()
, though the documentation doesn't tell you:
df = pd.DataFrame({'val':[1,11,130,670], 'x':[1,11,150,900]})
df.round({'val':-2})
This will round the column val
to the nearest 100, but leave x
alone.
Related Topics
How to Convert an Image to Black and White in PHP
How to Convert All Images to Jpg
PHP - Listing All Directories and Sub-Directories Recursively in Drop Down Menu
Replace Ereg_Replace with Preg_Replace
How to Force an Image Download in the Browser
Upload Images on Twitter Using PHP
Retrieving the Last Inserted Ids for Multiple Rows
Assets Not Referencing to Public Folder (Laravel)
Replacing Variables in a String
How to Send Email with Smtp in PHP
How to Loop Through a Multidimensional Array Without Knowing It's Depth
PHP Artisan Migrate Throwing [Pdo Exception] Could Not Find Driver - Using Laravel
Best Way to Internationalize Simple PHP Website
Message: Trying to Access Array Offset on Value of Type Null
How to Employ "If Exists" for Creating or Dropping an Index in MySQL