The round
function can take negative digits to round to, which causes it to round off to the left of the decimal. For example:
>>> round(15768, -3)
16000
>>> round(1218, -3)
1000
So the short answer is: Call round
with the second argument of -3
to round to the nearest 1000.
A minor warning since it surprises people: Python 3’s round
uses round-half-even rounding (also known as banker’s rounding), and you’re more likely to see “halves” when rounding to the left of the decimal. Basically, if input is exactly half-way between two possible values to round to, it will choose the value whose low non-zero digit is even. So:
>>> round(5, -1) # Equally close to 0 and 10, but 1 is odd, so 0 chosen
0
>>> round(15, -1) # Equally close to 10 and 20, but 1 is odd, so 20 chosen
20
On Python 2, it uses round-half-away-from-zero (and the result is always a float
, even when rounding to negative digits), which is what most people were taught in school (but produces larger overall error when many values are rounded) so the same calls would produce 10.0
and 20.0
(and round(-5, -1)
and round(-15, -1)
would produce -10.0
and -20.0
, where Python 3 would get 0
and -20
).