Tip: Use round() with negative arguments

Patrick Loeber

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 *

* This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you! 🙏

Check out my Courses