If you are working with many numbers in Python, you probably need to do some rounding to cut off some digits that you may not need at all.
For example, let us say that you are calculating an average salary in a company and the average number that you have found is around 54,334.218
This may not be a number that can look good in a final report you want to submit to someone. You should instead round it.
Let us do it in Python:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As you can see, the number 2 after the comma represents the number of digits we want to keep. The thing is, we can also use negative numbers to specify the number of digits we want to keep.
If we use -1, we are doing the rounding to the nearest ten:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When we need to do the rounding to the nearest hundert, we use -2:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If we want to do the rounding to the nearest thousand, we use -3:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I am an experienced and passionate Senior Software Engineer with a demonstrated history of working in the full life cycle of software development with enormous curiosity for data science, machine learning, algorithms, data structures, and solving challenging problems. I am an open-source enthusiast at https://github.com/fatosmorina and also a writer.
I am open for new opportunities.