If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer. In this Python Tutorial, you will learn:

Round() Syntax:
How much Impact can Rounding Have? (Rounding vs Truncation)
Example: Rounding Float Numbers
Example: Rounding Integer Values
Example: Rounding on Negative Numbers
Example: Round Numpy Arrays
Example: Decimal Module

Syntax:

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:

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

Output:

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 Output:

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. Output:

Example: Rounding on Negative Numbers

Let us see few examples of how rounding works on negative numbers Output:

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() Output: 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 Output:

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.