Updated March 24, 2023
Introduction to Python Round Function
The round function is a built-in function in Python. The theme of the round function is to return a float number. According to input by the user, this float number is rounded off to how many ever decimal places, according to input. The nearest integer value is returned when no specific decimal places to be rounded is given. And thus considered as zero. The round function helps in improving the floating-point numbers.
Suppose 4.5 is to round off to the nearest possible integer; it has to output 5. However, 4.7 to one decimal place will be the output for rounding off the number 4.74. Quick and ready rounding off the numbers is important when someone is working with floats. Mainly those digits which have many decimal places. Python round() is there to help it make it quicker and simpler.
- float number represents the number that needs to be rounded.
- The number of decimals specifies to which the float number is to be rounded off. The return type is float.
- If the number of decimals is not specified, zero is taken as a default, making it optional in specificity. Default zero is rounded off to the nearest integer, and the return type is considered an integer.
- For >=5 condition, +1 is added.
- For <5 condition, the final value is returned as it is up to the mentioned decimal places.
Examples of Python Round Function
Given below are the examples of Python Round Function:
Example #1 – Only with the first parameter.
#Integers: a = 12 round (a) print (a) #Floating Point b = 21.7 c = 21.4 print(round(b)) print(round(c))
Here, the integer is as it is, whereas the float number is rounded off to the nearest possible.
Example #2 – When both the parameters are used.
# when the (n+1)th digit is equal to 5 a = 5.465 print(round(a, 2)) # when the (ndigit+1)th digit is >=5 b = 5.476 print(round(b, 2)) # when the (ndigit+1)th digit is <5 c = 5.473 print(round(c, 2))
Practical Application where Round Function is Used
Examples of practical application where the round function is used are given below:
Example #1 – Round function saves the day where:
Fractions and decimals always have a mismatch. To handle such cases, rounding functions are used when fractions are being converted to decimals. After a decimal point, usually, there are many digits, such as for 22/7(Pi). But we use like a maximum of two to four digits after the decimal point. Type of built-in supporting round function, numbers will be rounded to multiple closest to 10 to power –digits.
The round(3.675, 2) gives 3.67 as output instead of 3.68. This can be surprising in Python. This is not considered a bug. The result characterizes the fact that most of the decimal fractions can not be predicted precisely as a float.
a = 1/6 print(a) print(round(a, 2))
Example #2 – Exceptions and errors.
Round function rounds 2, 2.5, 1.5 down to 2. This is not considered a bug rather;, the function behaves this way.
a = 1.5 b = 2 c = 2.5 print(round(a)) print(round(b)) print(round(c))
In the big picture, it works something like this:
#This is how round functions works in Python Tup = (-40.95, 50.85, 10.98, 20.26, 30.05) # Declaration of a Tuple Lis = [-39.29, -42.15 , -39.97, -10.98, 32.65] #Declaration of a List print('Round Function on Negative Decimal = %.2f' %round(-19.48476)) print('Round Function on Positive Decimal = %.2f' %round(15.98763)) print('Round Function with 2nd parameter on positive = %.3f' %round(11.98763, 3)) print('Round Function with 2nd parameter on negative = %.3f' %round(-18.48476, 3)) print('Round Function for Items in a List = %d' %round(Lis)) print('Round Function for Items in a List = %d' %round(Lis)) print('Round Function for Items in a Tuple = %d' %round(Tup)) print('Round Function for Items in a Tuple = %d' %round(Tup)) print('Round Function for Multiple Number = %.2f' %round(20 + 40 - 20.6578, 2))
There are diverse methods of rounding function in Python. One of such is:
Truncate is to shinken or shorten things. This method is considered the simplest method to round off a given number to a given number of digits involving truncatination. Being replaced with a given position, all the other digits are considered zero. Bypassing negative values, Truncate() can truncate the digits on the left side of the decimal point.truncate() function is used the same as of Python round function. Given with arguments a number and a number to be rounded to a given number of digits.
For example, truncate(565.5556, -2) is used as a truncate function.
The round function is easier and quicker when you are handing with huge data. The round number is returned to n digits precision after the decimal point. A nearest possible integer is returned to its input.
This has been a guide to Python Round Function. Here we discuss the introduction, examples and practical application of python round function. You may also have a look at the following articles to learn more –