Python round() built-in function

From the Python 3 documentation

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

Introduction

The round() function rounds a number to a specified number of decimal places. If the number of decimal places is not specified, it rounds to the nearest integer.

Note that round() uses “round half to even” for numbers ending in .5, which means it will round to the nearest even integer.

Examples

# Round to the nearest integer
print(round(3.14))   # Output: 3
print(round(3.8))    # Output: 4

# Round to a specified number of decimal places
print(round(3.14159, 2)) # Output: 3.14

# "Round half to even"
print(round(2.5)) # Output: 2
print(round(3.5)) # Output: 4

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.