Python round() Function Tutorial

Python Round() Function

This is a detailed tutorial of the Python round() Function. Round off numerical values to simple integers or upto a specified number of decimal points.

Python round() Function

The Python built-in function round() is used to round-off floating-point values (Numerical values with decimals) into either simple integers or floating-point values upto a specified number of decimals.

Syntax

round() Parameters

The round() method has two parameters, numericalValue, and numberOfDigits.

The floating-point number that you wanted to be rounded off has to be provided as the first argument of this function and it is the only required parameter.

In the second argument, you can specify the number of decimal digitals upto which you wanted the given number to be rounded off. It is optional to be provided and the default value for this parameter is 0. In other words, if you won’t specify the number of digits upto which the number has to be rounded off, it will automatically assume that the number has to eliminate all of the decimal values and the resultant rounded-off number will be a simple integer.

round() Return Type

The return type is the rounded off numerical value. It could be an integer as well an floating-point value.

If you will specify only the first argument to this function it will return an integer value closest to the number provided as an argument to this function. But if you will specify the number of digits more than 0 as the second argument to this method, you will get a floating-point value returned which will contain the same number of decimals as specified in the second argument.

Examples

Here we’ve shown the use of the function round() for the different numerical data types and values.

Example 1. Rounding Off To Integers in Python

If you want any floating-point numerical value into simple integers, you can make use of the round() function and you need not provide the second argument as it defaults to 0. The following example illustrates for integers as well as a floating-point number.

Python Round() Function Example

Example 2. Round-Off Upto Specified Number of Decimals

In the following code snippet, we’re also specifying the second argument to the round() function to round-off the given floating-point numbers upto the specified number of decimals.

Python Round() Method Rounding Off Upto A Specified Number Of Decimals

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *