Wednesday, December 25, 2024
Google search engine
HomeLanguagesround() function in Python

round() function in Python

Python round() function is a built-in function available with Python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.

Example 

In this example, we are using the round function for the number 111.23 in Python.

Python3




number = 111.23
rounded_number = round(number)
print(rounded_number)


Output :

111

Python round() Function Syntax

Syntax : round(number, number of digits)

Parameters : 

  • number : number to be rounded
  • number of digits (Optional) : number of digits up to which the given number is to be rounded.

If the second parameter is missing, then the round() function returns

  1. if only an integer is given, for instance, 15, then it will round off to 15 itself.
  2. if a decimal number is given, then it will round off to the closest multiple of 10 to the power minus ndigits.

Returns : The round() function always returns a number that is either a float or an integer.

Python round() Example

Python round() function if the Second parameter is Missing

In the given example, we have rounded off the 51.6,51.5,51.4 in Python.

Python3




# for integers
print(round(15))
 
# for floating point
print(round(51.6))
print(round(51.5))
print(round(51.4))


Output: 

15
52
52
51

When the second parameter is present, then it returns: 

The last decimal digit till which it is rounded is increased by 1 when (ndigit+1)th digit is >=5, else it stays the same.

Python round() function if the Second Parameter is Present

In the given example, we have rounded off the different numbers to the digit 2 decimal places.

Python3




# when the (ndigit+1)th digit is =5
print(round(2.665, 2))
 
# when the (ndigit+1)th digit is >=5
print(round(2.676, 2))
 
# when the (ndigit+1)th digit is <5
print(round(2.673, 2))


Output: 

2.67
2.68
2.67

Python round() with Negative Integers 

In the given example, round(-3.2) is converted into -3 is the nearest integer to -3.2. Similarly, round(-4.7) returns -5 since -5 is closer to -4.7 than -4. Similarly round(-2.5) returns -2 because it rounds down when the decimal part is exactly 0.5. Same as the fourth example demonstrates using the ndigits parameter with a negative number. round(-2.675, 2) returns -2.67.Similarly, round(-1234, -2), returns -1200 because it rounds to the nearest hundred, which is in the negative direction.

Python3




print(round(-3.2)) 
print(round(-4.7))
print(round(-2.5))
print(round(-2.675, 2))
print(round(-1234, -2))


Output :

-3
-5
-2
-2.67
-1200

Rounding Number with Math Library in Python

By default, round() rounds a number to the nearest integer. However, you can also specify whether to round up or down using the round() function in combination with the math module.

In the given example, we are rounding up and rounding down the number 3.6 in Python.

Python3




import math
 
num = 3.6
rounded_num = math.floor(num) # rounds down to nearest integer
print(rounded_num) # output: 3
 
rounded_num = math.ceil(num) # rounds up to nearest integer
print(rounded_num) # output: 4


Output :

3
4

Rounding Number with Numpy Module In Python

In this example, we are using numpy module to round the values to their 3rd decimal places in Python.

Python3




import numpy as np
 
arr = np.array([-2.675, -1.23456789, -3.14159265])
rounded_arr = np.round(arr, decimals=3)
 
print(rounded_arr)


Output :

[-2.675 -1.235 -3.142]

Round Up Numbers in Python

In the given example, we have rounded off the number 12.7.

Python3




print(round(12))
print(round(12.7))


Output:

12
13

Round Down Numbers in Python

In the given example, we have rounded off the numbers 12.1,12.4,12.5.

Python3




print(round(12))
print(round(12.1))
print(round(12.4))
print(round(12.5))


Output:

12
12
12
12

Error and Exceptions

TypeError: This error is raised in the case when there is anything other than numbers in the parameters. 

Python3




print(round("a", 2))


Output: 

Runtime Errors:
Traceback (most recent call last):
  File "/home/ccdcfc451ab046030492e0e758d42461.py", line 1, in 
    print(round("a", 2))  
TypeError: type str doesn't define __round__ method

Practical Applications

One of the common uses of rounding functions is Handling the mismatch between fractions and decimals. We usually work with just two or three digits to the right of the decimal point when there is no exact equivalent to the fraction in decimal.

Python3




# practical application
b = 1/3
print(b)
print(round(b, 2))


Output: 

0.3333333333333333
0.33

Note: In Python, if we round off numbers to floor or ceil without giving the second parameter, it will return 15.0 for example and in Python 3 it returns 15, so to avoid this we can use (int) type conversion in Python. It is also important to note that the round ()function shows unusual behaviour when it comes to finding the mean of two numbers. 

RELATED ARTICLES

Most Popular

Recent Comments