Skip to content

Python round to nearest integer

  • by

Use the round() function to round to the nearest integer in Python. It will round up your number to your desired decimal place.

round(number, digits) 

The second argument is optional and defaults to 0 when not specified, and in such case, it will round to the nearest integer, and the return type will also be an integer.

Python round to nearest integer example

Simple example code.

# for integers
print(round(10))

# for floating point
print(round(10.6))
print(round(10.5))
print(round(10.40))

Output:

Python round to nearest integer

Using the second parameter in the round function

# when the (ndigit+1)th digit is =5
print(round(1.665, 2))

# when the (ndigit+1)th digit is >=5
print(round(1.676, 2))

# when the (ndigit+1)th digit is <5
print(round(1.673, 2))

Output:

1.67
1.68
1.67

Do comment if you have any doubts or suggestions on this Python round topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *