Original Source Here

# How to Round Numbers Using Negative Numbers in Python

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 that you want to submit to someone. It would help if you instead rounded it.

Let us do it in Python:

`print(round(54334.218, 2)) # 54334.22`

As you can see, the number 2 after the comma represents the number of digits we want to keep. 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:

`print(round(12346, -1)) # 12350`

When we need to do the rounding to the nearest hundred, we use -2:

`print(round(12346, -2)) # 12300`

If we want to do the rounding to the nearest thousand, we use -3:

`round(12346, -3) # 12000`

This may seem something quite trivial, but I also got to learn it just recently.

I hope you find this helpful.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot