While working on the arithmetic expression, we often get the results of floating-point numbers. However, we sometimes want a specific precision while denoting those floating-point numbers as we wish to only two digits after the decimal. Today in this article, we will discuss how we can round off to two decimal places in Python.
The rounding of the number in Python is quite simple using the round() function. So, let’s move ahead and see that.
Rounding to two decimals using round() Function
This is another most straightforward way to set precision over decimal numbers. Round Function rounds the decimal number to the precision we defined inside its argument. It takes two values as the argument. The first one is the number itself and the second one is the precision point of the number. Let’s see an example of it.
decimal_number = 3.1746783
print("Original number:",decimal_number)
print ("The number after specifying 2 decimal position is ",round(decimal_number,2))
Output:
Original number: 3.1746783
The number after specifying 2 decimal position is 3.17
So, in the above example, you can see that as the third digit after the decimal is less than 5 ( >5 ), it rounds it with the same digit. However, what if it is greater than 5? Let’s see that.
decimal_number = 3.176
print("Original number:",decimal_number)
print ("The number after specifying 2 decimal position is ",round(decimal_number,2))
Output:
Original number: 3.176
The number after specifying 2 decimal position is 3.18
So, in this case, you can see that the second digit increases by 1 unit. This happens because while rounding off the third number, the digit is greater than 5.
Rounding to two decimals using new style string formatting
However, we can also round the numbers using string formatting. To do that, we need to use the format() method where we have to pass the floating-point number as the argument and have to define the precision inside the {.2f}. Let’s see that.
print("{:.2f}".format(1.2385))
Output:
1.24
In the above example, you can see that the third digit after the decimal is greater than 5, which implies that rounding off the second digit (after the decimal) should be increased by one unit. You can verify it with the output. However, what happens if the third digit is less than 5. Let’s see that.
print("{:.2f}".format(1.2335))
Output:
1.23
Rounding to two decimals using Decimal module
Besides the above methods, we also have decimals which can help us round off the numbers. However, the above methods are easy to use and are very handy compared to this method. Let’s see that. This module has different rounding-off modes that are used as per our requirements. Let’s see other ways of rounding in this module.
ROUND_CEILING
This rounding mode rounds the number toward infinity which means that the result of rounding always exceeds the number passed. Let’s see an example.
import decimal
from decimal import Decimal
from decimal import getcontext
getcontext().prec = 3
getcontext().rounding = decimal.ROUND_CEILING
print (Decimal(3.921) + Decimal(0))
print (Decimal(3.929) + Decimal(0))
print (Decimal(-3.921) + Decimal(0))
print (Decimal(-3.929) + Decimal(0))
Output:
3.93
3.93
-3.92
-3.92
ROUND_FLOOR
This rounding mode rounds the number toward –infinity. It means that the result of rounding never exceeds the number passed.
import decimal
from decimal import Decimal
from decimal import getcontext
getcontext().prec = 3
getcontext().rounding = decimal.ROUND_FLOOR
print (Decimal(3.921) + Decimal(0))
print (Decimal(3.929) + Decimal(0))
print (Decimal(-3.921) + Decimal(0))
print (Decimal(-3.929) + Decimal(0))
Output:
3.92
3.92
-3.93
-3.93
ROUND_UP
This method is used to round off the number away from zero.
import decimal
from decimal import Decimal
from decimal import getcontext
getcontext().prec = 3
getcontext().rounding = decimal.ROUND_UP
print (Decimal(3.921) + Decimal(0))
print (Decimal(3.929) + Decimal(0))
print (Decimal(-3.921) + Decimal(0))
print (Decimal(-3.929) + Decimal(0))
Output:
3.93
3.93
-3.93
-3.93
ROUND_DOWN
This is another rounding mode where the rounding result gets closer to zero. The result of a negative number exceeds the original number, and the result of the positive number succeeds the original number.
import decimal
from decimal import Decimal
from decimal import getcontext
getcontext().prec = 3
getcontext().rounding = decimal.ROUND_DOWN
print (Decimal(3.921) + Decimal(0))
print (Decimal(3.929) + Decimal(0))
print (Decimal(-3.921) + Decimal(0))
print (Decimal(-3.929) + Decimal(0))
Output:
3.92
3.92
-3.92
-3.92
ROUND_HALF_UP
This mode is used with the ties and yields output going away from zero.
import decimal
from decimal import Decimal
from decimal import getcontext
getcontext().prec = 3
getcontext().rounding = decimal.ROUND_HALF_UP
print (Decimal(3.935) + Decimal(0))
print (Decimal(-3.935) + Decimal(0))
Output:
3.94
-3.94
ROUND_HALF_DOWN
This mode is used with the ties and yields output going towards zero.
import decimal
from decimal import Decimal
from decimal import getcontext
getcontext().prec = 3
getcontext().rounding = decimal.ROUND_HALF_DOWN
print (Decimal(3.925) + Decimal(0))
print (Decimal(-3.925) + Decimal(0))
Output:
3.92
-3.92
ROUND_HALF_EVEN
This rounding-up mode results in the output to the nearest even number in the tie.
import decimal
from decimal import Decimal
from decimal import getcontext
getcontext().prec = 3
getcontext().rounding = decimal.ROUND_HALF_EVEN
print (Decimal(3.925) + Decimal(0))
print (Decimal(3.935) + Decimal(0))
print (Decimal(3.933) + Decimal(0))
print (Decimal(-3.925) + Decimal(0))
print (Decimal(-3.935) + Decimal(0))
Output:
3.92
3.94
3.93
-3.92
-3.94
FAQs on Python Round to Two Decimals
This is the format specifier for the format function used to specify the precision while using floating-point numbers. Here, 2f means setting precision to two decimal places.
We can use the round() function to set our precision to two decimal places. Besides that, we can also use a format specifier with the format method.
To do that, you can use Decimal Class to initialize any decimal number.Decimal(3.14)
Output:
Decimal('3.140000000000000124344978758017532527446746826171875')
Conclusion
So, today in this article, we have seen how we can round to two decimal numbers in Python. We have seen the round() function that helps us in getting desired precision over the decimal number and rounding it two defined decimal places. Besides that, we have seen how we can round off a number using the format method and the Decimal module. In the decimal module, we have seen different modes for rounding off the numbers. I hope this article has helped you. Thank You.