round() function in Python
Last Updated :
07 Aug, 2024
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. In this article, we will see Python how to Round Numbers using round() function.
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:
- if only an integer is given, for instance, 15, then it will round off to 15 itself.
- 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() Function with Examples
The `round()` function in Python is used to round numbers. It takes two parameters: the number to be rounded and, optionally, the number of decimal places. If no decimal places are specified, it rounds to the nearest integer. The function follows standard rounding rules.
There are various method in Python how to Round Numbers, here we are explaining some generally used method which we used to round() function.
- Round Numbers Using Python round() Function
- Python round() function if the Second parameter is Missing
- Python round() function if the Second Parameter is Present
- Round Number with Math Library in Python
- Rounding Number with Numpy Module in Python
- Round Up Numbers in Python
- Round Down Numbers
Python round() Function
In this example, we are using the round function for the number 111.23 in Python.
Python
number = 111.23
rounded_number = round(number)
print(rounded_number)
Output :
111
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.
Python
# 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.
Python
# 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.
Python
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
Round 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.
Python
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.
Python
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.
Python
print(round(12))
print(round(12.7))
Output:
12
13
Python how to Round Down Numbers
In the given example, we have rounded off the numbers 12.1,12.4,12.5.
Python
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.
Python
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.
Python
# 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.
round() function in Python – FAQs
What is the Round Function in Python?
The round()
function in Python is a built-in function used to round a floating-point number to a specified number of decimal places. The default behavior without specifying decimal places rounds to the nearest whole number.
Syntax:
round(number, ndigits)
number
: the number you want to round.ndigits
: the number of decimal places to round to. If omitted, it rounds to the nearest whole number.
What is the Use of round()
?
The primary use of round()
is to reduce the number of decimal places to a more manageable or required level for display, calculation, or further processing. This function helps in controlling the precision of floating-point arithmetic and is commonly used in financial calculations, data summaries, and graphical data representations.
What is Round Formula Function?
In programming, the “round formula” typically refers to the algorithm used by the round()
function to determine how to round a given number to the nearest value with the specified precision. In Python:
- If the decimal part of the number is less than 0.5, the number is rounded down.
- If the decimal part of the number is 0.5 or more, the number is rounded up.
What is Round and ABS Function in Python?
round()
: As described, it’s used for rounding numbers to a specified precision.abs()
: The abs()
(absolute) function in Python returns the absolute value of a given number. The absolute value of a number is its non-negative value without regard to its sign. For instance, abs(-7.25)
returns 7.25
.
Example of round()
and abs()
:
print(round(3.7564, 2)) # Rounds to 3.76
print(abs(-3.7564)) # Returns 3.7564
Why Does Python Round 0.5 to 0?
Python uses a method called “rounding half to even” for its round()
function. When a value is exactly halfway between two integers, it rounds to the nearest even integer. This method reduces bias in rounding that can accumulate in statistical processes and is the default method in many programming languages.
Example of Rounding 0.5 in Python:
print(round(0.5)) # Returns 0
print(round(1.5)) # Returns 2