How to Round Floating Value to Two Decimals in Python

In this article, we will learn to round of floating value to two decimal places in Python. We will use some built-in functions and some custom codes as well. Let's first have a quick look over what are Python variables and then how to round off is performed in Python.

Python Float Type

These are floating point real values and are also called floats. Floating-point numbers are written with a decimal point separating the integer part and the fractional part.

Here are some examples of float values- 1.23. 1.08, 45.60, 45.6123004, 1.23e5 (This represents scientific notation of float value where the decimal part is Mantissa and the exponential part(e) is Exponent).

Python Float Type Example

Now, let us print a float number and observe the following output.

Take a variable x, store any float value and print the variable x using the print statement.

x = 1.234
print(x)


1.234

Now, if you observe the above output of the print statement, you will see that there are three decimal places after the decimal point. A question arises here that, what if the programmer needs to print only the float value to a given number of decimal places.

Like for example, in the above code, x = 1.234. The programmer needs to print only two decimal places after the decimal point i.e. x value should be 1.23. So for this purpose, we can use the round() function.

Round to Two Decimals using round() Function

The round() function is a built-in function that is used to get a floating-point number rounded to the specified number of decimals.

Syntax

round(number, digit)

If digit (second parameter) is not given, it returns the nearest integer to the given number, else it returns the number rounded off to the given digit value.

# for integers
print(round(10))

# for floating point
print(round(10.7,2))
print(round(5.5678,2))
print(round(-12.481,2))


10
10.7
5.57
-12.48

In integer, there is no decimal point so the answer remains the same. In floating points, 10.7 gives 10.7 as the precision value is set to 2 decimal places while in 5.5678, the value changes to 5.57. This value rounded off to two decimal places.

Round to Two Decimals using format() Function

We can use str.format() function to display float value with two decimal places. It keeps the actual value intact and is simple to implement.

Syntax

{:0.2f}, .format(number)

Example 1

print("Sir, your shopping bill amount is Rs. {:0.2f}.".format(206.551))


Sir, your shopping bill amount is Rs. 206.55.

Example 2

We can also use % instead of format() function to get formatted output. It is similar to the format specifier in the print function of the C language. Just use the formatting with %.2f which gives you round down to 2 decimal points.

number= 7.361

print("\"Sir, your shopping bill amount is Rs. %.2f ." % number)


Sir, your shopping bill amount is Rs. 7.36.

Round to Two Decimals using the ceil() Function

Python math module provides ceil() and floor() functions to rounded up and rounded down the any value. The floor and ceiling functions generally map a real number to the largest previous or smallest following integer which has zero decimal places. So to use them for 2 decimal places the number is first multiplied by 100 to shift the decimal point and is then divided by 100 afterward to compensate.

Example

#using round fucntion
round(2.357, 2)  


#using math.ceil() and math.floor()
import math
num = 2.357
print(math.ceil(num*100)/100) 
print(math.floor(num*100)/100) 


2.36
2.36
2.35

Round to Two Decimals using the decimal Module

Python provides a decimal module that contains several functions to deal with decimal values. We can use it to round off float value to the two decimal points. This method of using decimals will round decimals with super rounding powers.

#import decimal
from decimal import Decimal,

value = Decimal(16.0/7)
result = Decimal(value.quantize(Decimal('.01'), rounding = ROUND_HALF_UP))
print(result)


2.29

Example

This is an example of rounding imports Decimal and round decimal by setting precision values explicitly.

#import decimal 
from decimal import getcontext, Decimal

# Set the precision
getcontext().prec = 3

# Execute 1/7, however cast both numbers as decimals
result = Decimal(16.0)/Decimal(7)

# Your output will return w/ 6 decimal places
print(result)


2.29

This precision takes the total number of digits to be printed. If prec = 2, then the output would be 2.3.

Note:

The behavior of round() for floats can be surprising. For example, round(2.675, 2) gives 2.67 instead of the expected 2.68.

It's a result of the fact that most decimal fractions can't be represented exactly as a float. When the decimal 2.675 is converted to a binary floating-point number, it's again replaced with a binary approximation, whose exact value is: 2.67499999999999982236431605997495353221893310546875. Due to this, it is rounded down to 2.67.

If you're in a situation where this precision is needed, consider using the decimal module, as given above.

Conclusion

In this article, we learned to round values to two decimal places by using several built-in functions such as math.ceil(), math.floor(), decimal module, round() etc. We used some custom codes as well.