In programming languages, rounding or reducing long floating-point digits is frequently used to quickly estimate the values. Rounding a number means simplifying it while maintaining its value close to its original value. For example, rounding off a number 47 to the nearest 10s will give us 50 because 50 is much closer than 40. To perform this task, the Python round() built-in function has been established.
Definition
- The Python round() method returns a floating-point number that has been rounded to the provided number of decimals.
- Python round() inbuilt function takes two numeric arguments, n and ndigits, and returns the number n rounded to ndigits.
Python round()
Python includes a built-in function called round(). It will return a float number that has been rounded to the decimal places specified as input. If the number of decimal places to round is not specified, it is assumed to be 0 and will be rounded to the nearest integer.
round() Syntax
The syntax followed by Python’s round() function is as follows:
round(number, ndigits)
round() Parameters
The round() function accepts 2 parameters:
number – float number to be rounded
ndigits (Optional) – integer value till which the number is to be rounded off. The default value is 0
Note – If the number – ndigit+1 after the decimal place given is
- >=5, then +1 will be added to the final value
- If <5, the final value will be returned as it is up to the mentioned decimal places.
Return value from round()
The function round() returns the
- If ndigits is not specified, the nearest integer to the given number is used
- If ndigits is provided, the number is rounded off to ndigits digits.
Example 1: How does round() work in Python?
Example
# Python program to illustrate round()
# rounding integer values
a = 10
print(round(a))
# rounding floating-point numbers
b = 6.8
c = 99.4
print(round(b))
print(round(c))
# rounding even choice
print(round(5.5))
# rounding negative numbers
print(round(-4.7))
print(round(-3.2))
Output
10
7
99
6
-5
-3
Example 2: Rounding to even side
# Python program to illustrate round()
print(round(9.5))
print(round(10.5))
Output
10
10
Note the behavior of rounding off an (x.5) float number. If both the even and odd sides are equally near to the number (x.5), then the even side will always be rounded. Hence, 9.5 rounds off to even number 10, whereas 10.5 rounds off to the closest even number 10.
Example 3: Round a number to the given number of decimal places
Example
# Python program to illustrate round()
# when (ndigit+1)th digit = 5
print(round(5.665, 2))Â # adds +1
# when (ndigit+1)th digit >= 5
print(round(5.676, 2))Â Â # adds +1
# when (ndigit+1)th digit < 5
print(round(5.673, 2))Â # rounded off to ndigit
Output
5.67
5.68
5.67
Anomalies in floating-point numbers
Example
# since (ndigit+1)th digit = 5, we will expect +1 to be added
# and output = 20.58, *But check the output*
print(round(20.575, 2))
Output
20.57
Strange, isn’t it? We just learned that the output should come as 20.58, but instead it is 20.57. The behavior of the round() function for floating-point numbers can be really surprising. This is not really a bug, rather an explanatory logic stands behind it.
If we convert the decimal 20.575 to its binary floating-point number, its approximation value is:
20.574999999999999289457264239899814128875732421875
Due to this, it is rounded off to 20.57
Always use caution while working with floating points, as slight changes in output might cause major issues down the road.
To avoid this situation, we can use the decimal module, which is specifically designed for handling floating-point arithmetic’s:
from decimal import Decimal
# normal float
print(round(20.575, 2))
# using Decimal (passing float as a string)
num = Decimal('20.575')
print(round(num, 2))
Output
20.57
20.58
Practical Applications of round()
Handling the mismatch between fractions and decimals is a common application of function rounding. When converting fractions to decimals, we usually obtain a lot of digits following the decimal point, such as 0.166666667 for 1/6, but we only utilize two or three digits to the right of the decimal point. The round feature comes in handy here.
Example
x = 1/6
print('Value of x:', x)
print('Round off value of x:', round(x, 2))
Output
Value of x: 0.16666666666666666
Round off value of x: 0.17
Frequently Asked Questions
Q1. How do you round a float in Python?
Rounding a number means simplifying it while maintaining its value close to its original value. The Python round() method returns a floating-point number that has been rounded to the provided number of decimals. Let us look at a simple example to understand how to round a float in Python.
Example
x = 20.3
print(round(x))
x = 20.6
print(round(x))
x = -45.7
print(round(x))
x = 5.5
print(round(x))
Output
20
21
-46
6
Q2. How do you round a float to 2 decimal places in Python?
To round a float to 2 decimal places we use the following method:
round(number, ndigits)
The round() function accepts 2 parameters:
number – float number to be rounded
ndigits (Optional) – integer value till which the number is to be rounded off. Default value is 0
Note – If the number – ndigit+1 after the decimal place given is
- >=5, then +1 will be added to the final value
- If <5 then the final value will be returned as it is up to decimal places mentioned.
Example
# (ndigits+1)th digit >= 5
x = 20.3472
print(round(x, 2))Â # adds +1, hence output 20.35
# (ndigits+1)th digit < 5
x = 20.6239
print(round(x, 2))Â # does not add +1, hence output 20.62
x = -45.793
print(round(x, 2))
x = -5.52849
print(round(x, 2))
Output
20.35
20.62
-45.79
-5.53
Q3. How do you round a float in Python 3?
Rounding a float value in Python 3 is similar to other versions of Python.
The syntax followed by Python’s round() function in Python 3 is as follows:
round(number, ndigits)
Example
print(round(4.7))
print(round(8.3))
print(round(-13.63))
print(round(-53.832))
print(round(10.68323, 3))
print(round(321.53884, 3))
Output
5
8
-14
-54
10.683
321.539
Leave a Reply