Python round()

The round() method returns the floating point number rounded off to the given ndigits digits after the decimal point. If no ndigits is provided, it rounds off the number to the nearest integer.

The syntax of round() is:

round(number[, ndigits])

round() Parameters

The round() method takes two parameters:

  • number - number that is to be rounded
  • ndigits (Optional) - number upto which the given number is to be rounded

Return value from round()

The round() method returns:

  • (If ndigits not provided) nearest integer to the given number
    If two multiples are really close, rounding is done toward the even choice
  • (If ndigits given) floating point number rounded off to the ndigits digits
    Always rounded to the closest multiple of 10 to the power minus ndigits

Example 1: How round() works in Python?

# for integers
print(round(10))

# for floating point
print(round(10.7))

# even choice
print(round(5.5))

When you run the program, the output will be:

10
11
6

Example 2: Round a number to given ndigits places

print(round(2.665, 2))

# cannot be represented exactly as float
print(round(2.675, 2))

When you run the program, the output will be:

2.67
2.67

Here, both numbers result to the same output 2.67, when 2.675 should have been rounded to 2.68.

This isn't a bug but it is because, most decimal numbers cannot be represented exactly as a float.