Rounds a number.
Synopsis
round(number)
round(number, digits)
Description
Without digits, round returns the nearest whole number. With digits, it keeps that many places after the decimal point.
An exact half rounds to the nearest even number: round(2.5) is 2, but round(3.5) is 4.
Parameters
| Name | Meaning |
|---|---|
number |
The number to round. |
digits |
How many places to keep after the decimal point. Optional. |
Return value
An int without digits, a float with it.
Example
print(round(3.7))
print(round(3.14159, 2))
print(round(2.5))
print(round(3.5))
It prints:
4
3.14
2
4
Common mistake
print(round(2.675, 2))
It prints:
2.67
You might expect 2.68. The float stored for 2.675 is a little less than 2.675, so it rounds down. To show money, format it instead: f"{x:.2f}".
See also
Last updated 2 weeks ago