round() Function in Python | Scaler Topics (original) (raw)

Overview

Round function in Python is an inbuilt function. It is used to round off numbers up to the specified number of decimal 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)

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 a required parameter for the round function and omitting this parameter gives an error. If any unsupported data type like 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

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, a floating-point number is returned.

Practical applications

Examples of Round off in Python

Example 1

Output

Example 2

Output

Python Round() Function Example With Float Numbers

Floating point numbers are rounded on the basis of the n_digits parameter.

Example

Output

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 points, it would become 650.

Let's have a look at some more examples with the help of the code below:

  1. Round off in python with a positive value of n_digits has no effect on the integer number.

Output

  1. Round off in python with zero value of n_digits also has no effect on the integer number.

Output

  1. 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 a digits of the number equal to zero while rounding off the other digits.

Output

Tie Breaking in Python Round() Function

In this section, we 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.

The above technique can be explained by the examples below:

  1. Rounding off integers in Python

Output

  1. The same technique is also applied when floating-point numbers are round off in python.

Output

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.

This is explained further by the examples below:

  1. Rounding off to negative decimal points

Output

  1. If the absolute value of negative decimal points is greater than the number of digits in the original number.

Output

Python Round() Function Example With Numpy Arrays

The round() function can also be used on numpy arrays in a similar way 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 values of the numpy array to given precision with a single line of code. The below examples will draw a clearer picture:

Example 1

Output

Example 2

Output

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:

Output

Output

Output

Output

Output

Output

Output

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:

  1. 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.

Output

  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.

Output

Usages of Python Round() Function

Python round() function has a lot of usages -

Conclusion