On this page
round
Prev: reversed | Up: __builtins__ | Next: set
Rel: abs, divmod, max, min, pow
Ref: docs.python.org — round
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import round
Overview & Signature
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
Usage
assert round(3.14159, 2) == 3.14
assert round(2.5) == 2 and round(3.5) == 4 # banker's rounding (round half to even)
assert round(1234, -2) == 1200 # negative ndigits rounds tens/hundreds
Gotchas
- Uses round-half-to-even ("banker's rounding"):
round(2.5)is2, not3—usedecimal.Decimal.quantize(..., ROUND_HALF_UP)if you need traditional half-up rounding. - Returns
intwhenndigitsis omitted,floatwhenndigitsis provided (round(3.14, 0) == 3.0).
REPL Inspect
help(round)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/abs Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/divmod Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/max Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/min Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/pow Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/reversed Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/set Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/c/builtins shared tags: #builtins