It is a built-in function provided by python to round up any integer. If we pass a decimal number to the python round function with round off digits, it will return the floating number with rounded off value. If no digits are passed as a second parameter, it will round off the number nearest integer.
Table of Contents
Syntax
round( input number, round off digits)
Parameters
- Input number – number to be rounded off. (Required)
- Round off digits – number of digits which input number is rounded off. (optional)
Examples of python round function
Passing an integer as a parameter to the python round function. It will return the same value.
Example 1
a = round(184)
b = round(957)
c = round(4397)
print(a)
print(b)
print(c)
Output
184
957
4397
Simple code of implementation round function with first parameter decimal and the second parameter is missing. Let’s see what its output is.
Example 2
a = round(5.4)
b = round(8.184)
c = round(9.34)
print(a)
print(b)
print(c)
Output
5
8
9
Passing negative integers as parameters to the round function.
Example 3
a = round(-10.5)
b = round(-54.34)
c = round(-37.2232)
print(a)
print(b)
print(c)
Output
-10
-54
-37
Python also provides ceil() function to round up the given number.
It takes the number and returns the number nearest integer that is greater than or equal to the given number. The ceil function is defined in the math library. So you should import the math library to use the ceil function in the program. Let’s see how it works.
Example
import math
a = math.ceil(2.5)
b = math.ceil(8.4)
c = math.ceil(9.1)
print(a)
print(b)
print(c)
Output:
3
9
10
Passing a negative integer to the ceil function will return a number nearest to the given number. An example of ceil function is given below.
Example
import math
a = math.ceil(-2.5)
b = math.ceil(-8.4)
c = math.ceil(-9.1)
print(a)
print(b)
print(c)
Output
-2
-8
-9
Exactly opposite working of ceil() python provides floor() function.
Python also provides a floor() function that works exactly the opposite of ceil function. The floor function returns an integer which is nearest that is less than or equal to the given number. It is defined in the math library. Let’s have a look at the working of the python floor function.
Example
import math
a = math.floor(2.5)
b = math.floor(8.4)
c = math.floor(6.8)
print(a)
print(b)
print(c)
Output
2
8
6
By passing a negative number as a parameter to it, it will return the nearest integer to the given number.
Example
import math
a = math.floor(-2.5)
b = math.floor(-8.4)
c = math.floor(-6.8)
print(a)
print(b)
print(c)
Output
-3
-9
-7
Using ceil and floor function we can make a user-defined function that will round up and round down the passed number as a parameter.
Code of user-defined python round up function.
The first parameter is a number to be rounded off and the second parameter is the number of digits to be rounded off. The second parameter is set 0 as default so it is an optional parameter.
import math
def round_up(num, n=0):
mult = 10 ** n
return math.ceil(n * mult) / mult
1. Passing a number to the user-defined python round up function and second parameter is missing.
Example
a = round_up(146.54)
b = round_up(-242.5432)
c = round_up(4934.24)
print(a)
print(b)
print(c)
Output
147.0
-242.0
4935.0
2. Passing number as a first parameter and number of round off digits as a second parameter.
Example
a = round_up(146.54 ,1)
b = round_up(-242.543223 ,2)
c = round_up(4934.24233231, 3)
print(a)
print(b)
print(c)
Output
146.6
-242.54
4934.243
Code for user-defined python round down function.
import math
def round_up(num, n=0):
mult = 10**n
return math.floor(num * mult) / mult
1. Passing a number to the user-defined python round down function and second parameter is missing.
Example
a = round_up(49.495793)
b = round_up(-23784.53)
c = round_up(28.321)
print(a)
print(b)
print(c)
Output
49.0
-23785.0
28.0
2. Passing number as a first parameter and number of round off digits as a second parameter.
Example
a = round_up(49.495793 ,4)
b = round_up(-23784.53 ,2)
c = round_up(28.321,1)
print(a)
print(b)
print(c)
Output
49.4957
-23784.53
28.3
FAQ’s
Python provides a predefined function namely round which rounds up the passed number as a parameter and returns it. It is defined in the math library
Simply pass a floating point number to the round function it will return the whole number. If you want to round off the digits after the decimal point passes the number of digits to be rounded off as a second parameter to the round function.
Python provides a floor function that is predefined in a math library which returns a given number nearest that is less than equal to the given number.
Python has a round function that rounds up the number and returns it. Or you can simply pass the number to the int(), an example of int() given below.
>>> int(234.4)
234
>>> int(342.5653)
342