Python round() function with EXAMPLES
Round()
Round() 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.
Syntax
round(float_num, num_of_decimals)
Parameters
- float_num: the float number to be rounded.
- num_of_decimals: (optional) The number of decimals to be considered while rounding. It is optional, and if not specified, it defaults to 0, and the rounding is done to the nearest integer.
Description
The round() method takes two argument
- the number to be rounded and
- the decimal places it should consider while rounding.
The second argument is optional and defaults to 0 when not specified, and in such case, it will round to the nearest integer, and the return type will also be an integer.
When the decimal places, i.e. the second argument, is present, it will round to the number of places given. The return type will be a float.
If the number after the decimal place given
- >=5 than + 1 will be added to the final value
- <5 than the final value will return as it is up to the decimal places mentioned.
Return value
It will return an integer value if the num_of_decimals is not given and a float value if the num_of_decimals is given. Please note the value will be rounded to +1 if the value after the decimal point is >=5 else it will return the value as it is up to the decimal places mentioned.
How much Impact can Rounding Have? (Rounding vs Truncation)
The best example to show the impact of rounding is for the stock exchange market. In the past i.e in the year 1982, the Vancouver Stock Exchange (VSE): used to truncate the stock values to three decimal places on each trade.
It was done almost 3000 times every day. The accumulated truncations lead to a loss of around 25 points per month.
An example of truncating the values versus rounding is shown below.
Consider the floating-point numbers generated below as stock values. Right now I am generating it for a range of
1,000,000 seconds between 0.01 and 0.05.
Examples:
arr = [random.uniform(0.01, 0.05) for _ in range(1000000)]
To show the impact of rounding, I have written a small piece of code wherein at first, you need to use the numbers up to only 3 decimal places, i.e. truncating the number after 3 decimal places.
I have the original total value, the total coming from truncated values and the difference between original and truncated value.
On the same set of numbers, I have been using round() method up to 3 decimal places and calculating the sum and the difference between the original value and the rounded value.
Here are the example and the output
Example 1
import random def truncate(num): return int(num * 1000) / 1000 arr = [random.uniform(0.01, 0.05) for _ in range(1000000)] sum_num = 0 sum_truncate = 0 for i in arr: sum_num = sum_num + i sum_truncate = truncate(sum_truncate + i) print("Testing by using truncating upto 3 decimal places") print("The original sum is = ", sum_num) print("The total using truncate = ", sum_truncate) print("The difference from original - truncate = ", sum_num - sum_truncate) print("\n\n") print("Testing by using round() upto 3 decimal places") sum_num1 = 0 sum_truncate1 = 0 for i in arr: sum_num1 = sum_num1 + i sum_truncate1 = round(sum_truncate1 + i, 3) print("The original sum is =", sum_num1) print("The total using round = ", sum_truncate1) print("The difference from original - round =", sum_num1 - sum_truncate1)
Output:
Testing by using truncating upto 3 decimal places The original sum is = 29985.958619386867 The total using truncate = 29486.057 The difference from original - truncate = 499.9016193868665 Testing by using round() up to 3 decimal places The original sum is = 29985.958619386867 The total using round = 29985.912 The difference from original - round = 0.04661938686695066
The difference between original and after truncating is 499.9016193868665, and from round, it is 0.04661938686695066
The difference seems to be very big, and the example shows how to round() method helps in calculating close to accuracy.
Example: Rounding Float Numbers
In this program, we will see how rounding words on floating numbers
# testing round() float_num1 = 10.60 # here the value will be rounded to 11 as after the decimal point the number is 6 that is >5 float_num2 = 10.40 # here the value will be rounded to 10 as after the decimal point the number is 4 that is <=5 float_num3 = 10.3456 # here the value will be 10.35 as after the 2 decimal points the value >=5 float_num4 = 10.3445 #here the value will be 10.34 as after the 2 decimal points the value is <5 print("The rounded value without num_of_decimals is :", round(float_num1)) print("The rounded value without num_of_decimals is :", round(float_num2)) print("The rounded value with num_of_decimals as 2 is :", round(float_num3, 2)) print("The rounded value with num_of_decimals as 2 is :", round(float_num4, 2))
Output:
The rounded value without num_of_decimals is : 11 The rounded value without num_of_decimals is : 10 The rounded value with num_of_decimals as 2 is : 10.35 The rounded value with num_of_decimals as 2 is : 10.34
Example: Rounding Integer Values
If you happen to use round() on an integer value, it will just return you the number back without any changes.
# testing round() on a integer num = 15 print("The output is", round(num))
Output:
The output is 15
Example: Rounding on Negative Numbers
Let us see few examples of how rounding works on negative numbers
# testing round() num = -2.8 num1 = -1.5 print("The value after rounding is", round(num)) print("The value after rounding is", round(num1))
Output:
C:\pythontest>python testround.py The value after rounding is -3 The value after rounding is -2
Example: Round Numpy Arrays
How to round numpy arrays in python?
To solve this, we can make use of numpy module and use numpy.round() or numpy.around() method, as shown in the example below.
Using numpy.round()
# testing round() import numpy as np arr = [-0.341111, 1.455098989, 4.232323, -0.3432326, 7.626632, 5.122323] arr1 = np.round(arr, 2) print(arr1)
Output:
C:\pythontest>python testround.py [-0.34 1.46 4.23 -0.34 7.63 5.12]
We can also use numpy.around(), which gives you the same result as shown in the example below.
Example: Decimal Module
In addition to the round() function, python has a decimal module that helps in handling decimal numbers more accurately.
The Decimal module comes with rounding types, as shown below :
- ROUND_CEILING: it will round towards Infinity,
- ROUND_DOWN: it will round the value towards zero,
- ROUND_FLOOR: it will round towards -Infinity,
- ROUND_HALF_DOWN: it will round to nearest value going towards zero,
- ROUND_HALF_EVEN: it will round to nearest with value going to nearest even integer,
- ROUND_HALF_UP: it will round to nearest with value going away from zero
- ROUND_UP: it will round where the value will go away from zero.
In decimal, the quantize() method helps to round to a fixed number of decimal places, and you can specify the rounding to be used, as shown in the example below.
Example:
Using round() and decimal methods
import decimal round_num = 15.456 final_val = round(round_num, 2) #Using decimal module final_val1 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_CEILING) final_val2 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_DOWN) final_val3 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_FLOOR) final_val4 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_DOWN) final_val5 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_EVEN) final_val6 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_UP) final_val7 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_UP) print("Using round()", final_val) print("Using Decimal - ROUND_CEILING ",final_val1) print("Using Decimal - ROUND_DOWN ",final_val2) print("Using Decimal - ROUND_FLOOR ",final_val3) print("Using Decimal - ROUND_HALF_DOWN ",final_val4) print("Using Decimal - ROUND_HALF_EVEN ",final_val5) print("Using Decimal - ROUND_HALF_UP ",final_val6) print("Using Decimal - ROUND_UP ",final_val7)
Output:
Using round() 15.46 Using Decimal - ROUND_CEILING 15.46 Using Decimal - ROUND_DOWN 15.45 Using Decimal - ROUND_FLOOR 15.45 Using Decimal - ROUND_HALF_DOWN 15.46 Using Decimal - ROUND_HALF_EVEN 15.46 Using Decimal - ROUND_HALF_UP 15.46 Using Decimal - ROUND_UP 15.46
Summary
- Round(float_num, Num_of_decimals) is a built-in function available with python. It will return you the float number that will be rounded to the decimal places which are given as input.
- float_num: the float number to be rounded.
- Num_of_decimals: It is the number of decimals to be considered while rounding.
- It will return an integer value if the num_of_decimals is not given and a float value if the num_of_decimals is given.