all Python 3Arithmetic & operatorsfunction
round()
numbers
Built-in rounding function. With one argument it returns an int; with ndigits it rounds to that many decimal places. It uses banker's rounding (to the nearest even).
Syntax
round(number[, ndigits])
round(2.675, 2) yields 2.67, and banker's rounding is not the culprit — the binary float actually stored is slightly below the decimal 2.675. For money use decimal.Decimal with quantize(). Also note that round(x) without ndigits returns an int while round(x, 0) returns a float (2.0), so "round to an integer" and "round to zero digits" are different requests.
Examples
print(round(3.7)) # → 4
print(round(3.14159, 2)) # → 3.14
print(round(2.5)) # → 2 (банковское округление к чётному)
print(round(3.5)) # → 4 (банковское округление к чётному)
print(round(12345, -2)) # → 12300 (до сотен)
print(round(0.1 + 0.2, 10)) # → 0.3 (компенсация float погрешности)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy