How to Round Floating values to two decimal in Python

Python is a high-level programming language that is used extensively for data science, machine learning, and web development. One common operation in data science is to round floating values to two decimal places. This operation is useful when dealing with financial data or any other numeric data that requires a certain level of precision. In this article, we will discuss various ways to round floating values to two decimal places in Python.

Method 1: Using the round() Function

The round() function is a built-in function in Python that is used to round off a given number to a specified number of decimal places.

Syntax for round() function

The syntax for the round() function is as follows:

Where number is the number to be rounded and ndigits is the number of decimal places to which the number should be rounded.

To round a floating value to two decimal places using the round() function, we can simply pass the floating value as the first argument and 2 as the second argument:

Example:

Output:

3.14

Explanation:

In the above code, we have assigned the value 3.14159265359 to the variable x. After that, we used the round() function to round off x to two decimal places and assigned the result to the variable rounded. After that, the result is printed to the console.

Method 2: Using the format() Method

Another way to round a floating value to two decimal places is to use the format() method. The format() method is a built-in method in Python that allows us to format a string in a specific way.

Syntax for format() Method

The syntax for the format() method is as follows:

Where n is the number of decimal places to which the number should be rounded and number is the number to be rounded.

To round a floating value to two decimal places using the format() method, we can use the following code:

Output:

3.14

Explanation:

In the above code, we have assigned the value 3.14159265359 to the variable x. After that, we used the format() method to round off x to two decimal places and assigned the result to the variable rounded. After that, the result is printed to the console.

Method 3: Using the Decimal Module

The decimal module in Python provides support for working with decimal numbers. This module provides a Decimal class that allows us to perform mathematical operations with a high degree of precision. To round off a floating value to two decimal places using the Decimal module, we can use the following code:

Output:

3.14

Explanation:

In the above code, we have imported the Decimal class from the decimal module. After that, we have assigned the value 3.14159265359 to the variable x. We created a Decimal object from x and used the quantize() method to round off the value to two decimal places. After that, the result is assigned to the variable rounded and printed to the console.

Method 4: Using the math.ceil() and math.floor() Functions:

The math.ceil() function is a built-in function in Python that returns the smallest integer greater than or equal the value of a given number. Similarly, the math.floor() function returns the largest integer less than or equal to the given number. We can use these functions to round off a floating value to two decimal places as follows:

Output:

3.15
3.14

Explanation:

In the above code, we have imported the math module and assigned the value 3.14159265359 to the variable x. After that, we have used the math.ceil() and math.floor() functions to round off x to two decimal places. The result is then assigned to the variable rounded and printed to the console.

Conclusion

In this article, we have discussed various ways to round floating values to two decimal places in Python. These methods include using the round() function, the format() method, the Decimal module, the math.ceil() and math.floor() functions, and the numpy.round() function. Each of these methods has its own advantages and disadvantages, and the choice of method depends on the specific requirements of the project. It is important to choose the appropriate method based on factors such as accuracy, performance, and ease of use.






Latest Courses