Tip - Use the round() function with negative arguments

You can use the round() function with negative arguments to round it to the nearest 10, 100, 1000, and so on.

The round function rounds a number to the given precision in decimal digits:

value = 34.185609

print(round(value, 2))
# 34.19
print(round(value, 3))
# 34.186

But it can also be used to round to the nearest, 10, 100, 1000, and so on, by using negative arguments:

value = 123456

print(round(value, -1))
# 123460
print(round(value, -2))
# 123500
print(round(value, -3))
# 123000
print(round(value, -4))
# 120000

This can be useful when you don't care about the exact value while analyzing large numbers, e.g., when looking at the population of a city.

You can learn more about precision handling in Python in this article.

FREE VS Code / PyCharm Extensions I Use

鉁 Write cleaner code with Sourcery, instant refactoring suggestions: Link*

PySaaS: The Pure Python SaaS Starter Kit

馃殌 Build a software business faster with pure Python: Link*

* These are affiliate link. By clicking on it you will not have any additional costs. Instead, you will support my project. Thank you! 馃檹