Four ways to output python with two decimal places:
a = 5.5461
Method 1: round(a ,2)
Method 2: float('%.2f' % a)
##Related recommendations: "Python video tutorial》
Method three: '%.2' %a##Method four:
from decimal import Decimal
Decimal('5.026').quantize(Decimal('0.00'))
When the output result needs to have two decimal places When using string format: '%.2f' % a is the best method, followed by Decimal.
Note:
1. It can be passed to Decimal integer or string parameters, but it cannot be floating point data, because floating point data itself is not accurate.
2. Decimal can also be used to limit the total number of digits in data.
The above is the detailed content of How to keep two decimal places in python output. For more information, please follow other related articles on the PHP Chinese website!