Round Function in Python
Learn via video course
Overview
Round function in python programming is an inbuilt function. It is used to round off numbers up to the specified number of decimals points. It is an important inbuilt function.
Introduction to Round Function in Python
Suppose you go to a store and buy multiple items, and the bill comes out to be of an amount with 2 decimal places. Ever wondered how rounding off is done for such a bill amount? The answer is the round function. Read along to know more.
The round function in python is used for rounding off numbers up to a specified number of digits after the decimal point. For example, 65.234 can be rounded off to 65.2. It is an inbuilt function in python and hence does not require any module to be imported for its usage.
Syntax of Round Function in Python
The round function has the following syntax:
round(number, n_digits)
where number is the number to be rounded off, and n_digits is the number of digits after the decimal point to be rounded off. The n_digits parameter is optional and can also be omitted.
Python round() function Parameters
The round function accepts two parameters:
1. Number: This is the number that is to be rounded off. It can be a number of any type, like floating point, integer, etc. This is a required parameter for the round function, and omitting this parameter gives an error. If any unsupported data type, like a string, is passed to this parameter, it gives an error like str doesn't define round.
2. n_digits: This is the number of digits after the decimal point to be retained after rounding off the number. This is an optional parameter and can be omitted. Omitting it does not give any error, and the code returns a value without any decimal points. The default value of this parameter is zero. This parameter can have any integer value(positive, negative, or zero).
Python round() Function Return value
The round function's return value depends upon the value of n_digits. The round() function always returns a number that is either a float or an integer.
According to the official python documentation, it returns a value rounded to the closest multiple of 10 to the power minus n_digits(10^-n_digits)
If the input number is an integer, the return value is also an integer.
If the input number is a floating-point number, the output depends on n_digits. If n_digits is not specified, an integer is returned, else a floating-point number is returned.
Practical applications
- Rounding off bill amounts - A bill amount is generally paid in integer values, so the round() function can be used to round off the bill amount.
- Rounding off scientific data - Scientific data is high precision data and thus needs to be in a required format with a fixed number of floating and integer digits. This precision can be achieved with the help of the round() function.
- Handling Engineering Data - Theoretical data in engineering may need adjustments to be used feasibly in real-world applications. The round() function can help round off the values for applications in such cases.
Examples of Round off in Python
Example 1
Here are some examples that explain the usage of the python round() function.
In the first example, we have rounded off the number 65.67756 to 3 decimal places, and by doing so, we get the output as 65.678.
Output
Example 2
Here we have not specified the n_digits parameter, so it is treated as zero, and we get an integer value. This is because the default value of the n_digits parameter is zero.
Output
Python Round() Function Example With Float Numbers
Floating point numbers are rounded based on the n_digits parameter.
If the number after the required number of decimal digits is greater than 5, rounding off is done towards the next greater value, else rounding off is done towards the lower value. For example, 6.76 will be rounded off to 6.8, whereas 6.74 will be rounded off to 6.7. The same can be verified by the example code below.
Example
In this example, we can see that the 67.764 has been rounded off to 67.76 i.e., towards the lower value because it has 4 in the end. It is also observed that 67.766 has been rounded off to 67.77 i.e., towards the greater value because it has 6 in the end.
Output
-
Number after rounding off: a = 67.76
-
Number after rounding off: b = 67.77
Python round() Function Example With Integer Values
Integer values are not much affected by the round function. Integers are affected only if the value of the n_digits parameter is less than zero. In such cases, the integer number gets less significant. For example, if 652 is rounded off to -1 decimal point, it would become 650.
Let's have a look at some more examples with the help of the code below:
- Round off in python with a positive value of n_digits has no effect on the integer number.
Here we can see that rounding off the integer 888 up to 2 decimal places has no effect on it as 888 is already free of decimal values.
Output
Number after rounding off: a = 888
- Round off in python with zero value of n_digits also has no effect on the integer number.
We can observe that rounding the integer 888 to zero decimal places has no effect as 888 already has zero decimal places.
Output
Number after rounding off: a = 888
- Round off in python with a negative value of n_digits makes the number less significant. If the value of n_digits is -a, it makes the last digits of the number equal to zero while rounding off the other digits.
Here we can see that rounding off the integer 888 to -1 decimal places makes it 890. This is because the round() function rounds off the value to 10^-(n_digits), and thus 888 is rounded off to 890.
Output
Number after rounding off: a = 890
Tie Breaking in Python round() Function
This section will discuss how the python round() function will break ties. A tie occurs when the last digit of a number is 5, and the last needs to be removed while rounding. Generally, the values are rounded to the nearest value, but 5 lies in the middle in this case. For example, 7.5 is equidistant from 7 and 8, so how should it be rounded off?
The Python round() function follows the half to even rounding strategy. In this strategy, the number is rounded to its nearest even integer.
For example, if we need to round off 7.5, it will be rounded off to its nearest even integer, 8. But if we need to round off 6.5, it will be rounded off to 6 as it is closer to 6.
This technique is used to break ties in python when a number is equally close to two integers. This technique is also part of the IEEE 754 standard.
The above technique can be explained by the examples below:
- Rounding off integers in Python
In this example, we can see that 8.5 is rounded to 8, as 8 is the closest even value to 8.5.
Similarly, we can see that 7.5 is rounded up to 8, the closest even value to 7.5.
Output
- Number after rounding off: a = 8
- Number after rounding off: b = 8
- The same technique applies when floating-point numbers are rounded off in python.
Here we can see that 8.85 has been rounded off to 8.8 as it is the nearest even value.
Similarly, 8.75 is rounded off to 8.8 as it is the nearest even value.
Output
- Number after rounding off: a = 8.8
- Number after rounding off: b = 8.8
Python round() Function Example With Negative Numbers
Rounding off a number to a negative number of decimal points is valid in python and it rounds off the right most digits of the number. Using negative numbers as n_digits parameter reduces the significance of the input number.
If the absolute value of the n_digits parameter is greater than the number of digits in the input number, we get zero as the output.
For example, if 888.1212 is rounded off to -1 decimal place, it returns 890 because the round() function rounds off a value to the closest multiple of 10^(-n_digits). In this case, 10^(-n_digits) is 10, and the nearest multiple of 10 closest to 888 is 890, so we get 890 as output.
This is explained further by the examples below:
- Rounding off to negative decimal points
In the first example, we are rounding off 67.746 to -1 decimal points which means that it will get rounded off to the nearest multiple of 10. So it gets rounded off to 70.
Similarly, in the second case, we are rounding off 1212 to -2 decimal points which means that it will get rounded off to the nearest multiple of 100. So it gets rounded off to 1200.
Output
- Number after rounding off: a = 70.0
- Number after rounding off: b = 1200
- If the absolute value of negative decimal points is greater than the number of digits in the original number.
In such cases, the input number becomes zero because the value of 10^(-n_digits) is very high as compared to the input, and thus zero comes out to be the nearest multiple of 10^(-n_digits).
For example, in the first case, we can see that rounding off 67.7641 to -3 decimal points means that it will get rounded off to the nearest multiple of 1000, which in this case comes out to be 0.
Similarly, in the second case, we can see that rounding off 888 to -5 decimal points means that it will get rounded off to the nearest multiple of 100000, which in this case comes out as 0.
Output
- Number after rounding off: a = 0.0
- Number after rounding off: b = 0
Python round() Function Example With Numpy Arrays
The round() function can also be similarly used on numpy arrays as it is used on numbers. We need to import the numpy module and use the function as numpy.round(input_array) instead of round(). This feature will round off all the numpy array values to give precision with a single line of code. This feature helps in rounding off many values with a single line of code. The below examples will draw a clearer picture:
1. Example 1
In this example, we can see that we have rounded off the whole array named input_arr to 2 decimal places using the round() function on a numpy array. In the output, we can see that all the array elements are rounded off to 2 decimal places.
Output
Rounded array is: [-0.34 1.46 4.23 -0.34 7.63 5.12]
2. Example 2
In this example, we can see that we have rounded off the whole array named input_arr to -1 decimal places using the round() function on a numpy array. In the output, we can see that all the elements of the array are rounded off to -1 decimal places, i.e. to a multiple of 10.
Output
Rounded array is: [10 20 30 40 20]
Python round() Function Example With Decimal Module
The decimal module can be used with the round function to handle decimal numbers more accurately. While using the decimal module we specify a rounding parameter to a flag to specify the type of rounding to be applied. The flags are a set of predefined rules as explained below.
DECIMAL MODULE has the following flags available:
- ROUND_CEILING - It can be specified using rounding=decimal.ROUND_CEILING. This flag rounds up a number towards infinity. It will always round off the value to the next higher permissible value.
In this example, we have specified the rounding using the ROUND_CEILING flag, and thus we can see that the value 288.8121 is rounded off to 288.82 i.e. the next higher permissible value.
Output
Rounded number is: a = 288.82
- ROUND_DOWN - It can be specified using rounding=decimal.ROUND_DOWN. This flag rounds up a number towards zero. It means that it will always round off a positive value to the next lower permissible value but a negative value to the next higher permissible value.
In this example, we have specified the rounding using ROUND_DOWN flag and thus we can see that the value 288.8121 is rounded off to 288.81 i.e. the next lower permissible value but -288.8121 is rounded off to -288.81 i.e. the next higher permissible value.
Output
-
Rounded number is: a = 288.81
-
Rounded number is: b = -288.81
-
ROUND_FLOOR- It can be specified using rounding=decimal.ROUND_FLOOR. This function rounds down a number towards negative infinity. This means it will always round a number to the next lower permissible value. The difference between ROUND_FLOOR and ROUND_DOWN is in the case of negative numbers. ROUND_DOWN rounds up a negative value to the next higher permissible value, whereas ROUND_FLOOR rounds down a negative number to the next lower permissible value.
In this example, we have specified the rounding using the ROUND_FLOOR flag and thus, we can see that the value 288.8121 is rounded off to 288.81 i.e. the next lower permissible value and also -288.8121 is rounded off to -288.82 i.e. the next lower permissible value.
Output
-
Rounded number is: a = 288.81
-
Rounded number is: b = -288.82
-
ROUND_HALF_DOWN - It can be specified using rounding=decimal.ROUND_HALF_DOWN. This function rounds down a number towards the nearest permissible number and if a tie occurs, it will be rounded towards zero.
In this example, we have specified the rounding using ROUND_HALF_DOWN flag and thus we can see that the value 287.5 is rounded off to 287 i.e. the nearest permissible value with the tie going towards zero.
Output
Rounded number is: a = 287
- ROUND_HALF_EVEN - It can be specified using rounding=decimal.ROUND_HALF_EVEN. This function rounds down a number towards the nearest permissible number and if a tie occurs, it will be rounded towards the nearest even value.
In this example, we have specified the rounding using ROUND_HALF_EVEN flag and thus we can see that the value 287.5 is rounded off to 288 i.e. the nearest permissible even value.
Output
Rounded number is: a = 288
- ROUND_HALF_UP - It can be specified using rounding=decimal.ROUND_HALF_UP. This function rounds down a number towards the nearest permissible number and if a tie occurs, it will be rounded away from zero, which means positive values will be rounded to the next higher value whereas negative values will be rounded down the next lower value.
In this example, we have specified the rounding using ROUND_HALF_UP flag and thus we can see that the value of a that is 287.5 is rounded off to 288 i.e. the next higher value whereas -287.5 is rounded down to -288 that is the next lower value.
Output
-
Rounded number is: a = 288
-
Rounded number is: b = -288
-
ROUND_UP - It can be specified using rounding=decimal.ROUND_UP. This function rounds a number away from zero irrespective of the tie condition. It will round off a positive number similar to the ceil() function. A positive number will be rounded off to the next higher value and a negative value will be rounded off to the next lower value.
In this example, we have specified the rounding using ROUND_UP flag and thus we can see that the value of a that is 287.1 is rounded off to 288 i.e. the next higher value whereas -287.9 is rounded down to -288 that is the next lower value.
Output
- Rounded number is: a = 288
- Rounded number is: b = -288
How to Round Away from Zero
If a value in the range [-0.5, 0.5] is rounded using the round() function of python, the answer leads to zero as it follows half to even rounding and rounds the number to the nearest integer value, which comes out to be zero for the range mentioned above.
To overcome this problem we can use the following solutions:
- For positive numbers, we can add 0.5 to our number. This ensures that our number is always greater than 0.5 and prevents it from being rounded to zero.
In this code, we can see that if we round off 0.3 directly, we get zero but if we add 0.5 to it, we get 1, thus preventing our number from being rounded off to zero.
Output
- Rounded number is: a = 0
- Rounded number with rounding away from zero: a = 1
- For negative numbers, we can subtract 0.5 from our number. This ensures that our number is always lesser than -0.5 and prevents it from being rounded to zero.
In this code, we can see that if we round off -0.3 directly, we get zero but if we subtract 0.5 from it, we get 1, thus preventing our number from being rounded off to zero.
Output
- Rounded number is: a = 0
- Rounded number with rounding away from zero: a = -1
Usages of Python round() Function
Python round() function has a lot of usages -
- It can be used wherever a need to round off a number to a fixed number of decimal points arises.
- It can be used to format outputs of programs, for example, if we need only 2 decimal points in all our outputs but due to mathematical calculations we are getting a different number of decimal places for each value, we can use round() function to bring our output to a uniform format.
- It can round off an array of values using numpy arrays uniformly.
- It can also be used to round off numbers to the required integer precision(252 can be rounded to 250)
- It cannot be used to round off a string value or a character array and in such cases, it gives an error.
Conclusion
- In this article, we have discussed the basic working of the round() function in python.
- We have also studied its required parameters and its return type.
- The usage of the round() function with different types of data like integer, float, etc. has also been discussed.
- The usage of the round() function with numpy arrays and with the decimal module is also explained.
- Finally, we have discussed how to round away from zero and also the various usages of the python round() function.