Python round() Function

Python round() Function

The Python round() function is another useful built-in Python function that can be used when doing computational work. The first parameter for the round() function is a numerical value and is required. The round() function can also accept an integer as an optional second input that specifies the number of digits after the decimal point to round to. When the function runs, the rounded value of the first input is returned. If you do not provide the second input or if its value is zero, the function rounds the given numerical value to the nearest integer. If the second input is provided, the function rounds the given value to the specified number of digits after the decimal point. In other words, when the second input is provided, round() rounds the given numerical value to the closest multiple of 10 to the power of negative N, with N being the second input. We can take a look at several examples of how the round() function works in Python now.


Python round() Example 1

In the first example of the round() function, we can pass a simple negative float value. Notice we only provide one argument to the function, we are omitting the optional second argument. We can see that applying round() to the value of -2.8 produces a value of -3.

python round function example


Python round() Example 2

In example two, this time we add the optional second argument to the round() function. Here we pass in -3.67 as the first argument and 1 as the second argument. This produces a result of -3.7.

round function in python


Python round() Example 3

The third example of using the round() function shows us testing out 2 decimal places during rounding. Passing in -4.877 with the second argument of 2 produces the result of -4.88.

example three python round


Python round() Example 4

No article discussing mathematics would be complete without mentioning some form of Pi. In example four we pass in 3.14 as the first argument with 1 as the second argument. The result here is 3.1

example four round python


Python round() Example 5

In the fifth example of the python round() function, we are rounding 3.141 to two decimal places. We get the expected 3.14 as output from the function.

round pi python


Python round() Example 6

Here we build on the prior example and instead of rounding to two decimal places, we round to three. The first argument to the function is 3.1415 with 3 as the second argument. The result of this round() call is 3.142.

three decimal places round


Python round() Example 7

The last example of round() takes in the number of 27.918273645 and rounds to 4 decimal places. The result we get is 27.9183.

round function


Python round() Function Summary

The Python round() function returns a floating-point number that is a rounded version of the given number, with the specified number of decimals. By default, the number of decimals is 0. So if you call round() with no second argument, the function will return the nearest integer.