Post

Python round()

The round() function returns the floating-point number that will be rounded to the given decimal number.

Python round()

The syntax of the round() function is :

1
round(number, digits)

round() Parameters

The round() function takes two parameters as arguments:

  • number - number to be rounded floating-point to return.
  • digits (optional) - The number of decimals to use when rounding the number. Default is 0

Let see some examples of the python round() function.

Example 1: How to use Python round() function?

1
2
3
4
5
6
7
8
9
# for integers
print(round(4))

# for floating point
print(round(13.1))

# even choice
print(round(7.7))

Output:

1
2
3
4
4
13
8

Example 2: How to round a number to the given number of decimal places?

1
2
3
print(round(4.738, 2))
print(round(4.778, 2))

Output:

1
2
3
4.74
4.78

Rules of round() function

  • If digits are not provided, round() returns the nearest integer to the given number.
  • If digits are given, round() returns the number rounded off to the digits.
This post is licensed under CC BY 4.0 by the author.