In Python, round(number) returns the nearest int, and round(number, ndigits) rounds to ndigits decimal places and keeps the number's type, so a float stays a float.
Exact halves are rounded to the nearest even digit (“round half to even”, also called banker's rounding): round(2.5) is 2 and round(3.5) is 4. Because floats are stored in binary, some values that look like halves are slightly below or above, so round(2.675, 2) gives 2.67.
round() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Leave out ndigits to get an int; pass it to keep decimals.
Exact decimal.Decimal("2.665") is stored exactly, so it really is a half at the third place.
round() uses half-even. The default decimal context rounds 2.665 to 2.66, because 6 is the even digit.
Choose the rule.quantize(..., rounding=ROUND_HALF_UP) applies school-style rounding and gives 2.67.
Formatting follows round().f"{2.5:.0f}" prints 2; .3f shows three places without changing the stored value.
Frequently Asked Questions
Python 3 rounds exact halves to the nearest even number (banker's rounding). round(2.5) is 2 and round(3.5) is 4. This avoids a bias toward larger numbers.
The float 2.675 is actually stored as 2.67499999999999982236431605997495353221893310546875, which is below the halfway point, so it rounds down.
Without ndigits it returns an int. With ndigits (even 0) it returns the same type as the input, so round(3.7, 0) is 4.0.
Use decimal.Decimal with quantize(Decimal("0.01"), rounding=ROUND_HALF_UP), or math.floor(x + 0.5) for non-negative numbers when exactness is not critical.
Use math.ceil() to round up and math.floor() to round down. int() truncates toward zero.