Spread the love
Rounding numbers is such a common tasks that Python lets you do that out of the box. There’s a built-in function for that, round. So, how do you round numbers in Python?
To round the number x to n decimal places we use the function like this:
round(x, n) – x rounded to n decimal places
The parameter n is optional. Its default value is 0, so round(x) has the same meaning as round(x, 0).
Some examples:
>>> round(3.5478745)
4
>>> round(3.5478745, 2)
3.55
>>> round(-1.12)
-1
>>> round(-1.12, 5)
-1.12
>>> round(-6.32441333547541)
-6
>>> round(-6.32441333547541, 1)
-6.3
Here’s the video version:
Spread the love