You can use the format
method on strings to specify how many decimal places you want to represent:
>>> "{:.2f}".format(1.5)
'1.50'
But even better would be to use the decimal module
for representing money, since representation issues with binary floats can give you slightly off results if you're doing arithmetic. The documentation for that module mentions some of those issues specifically - one of the most interesting ones for money applications is:
>>> 0.1+0.1+0.1-0.3
5.551115123125783e-17
>>> from decimal import Decimal
>>> Decimal('.1') + Decimal('.1') + Decimal('.1') - Decimal('.3')
Decimal('0.0')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…