Convert float 27.7 to 27.70 [closed]
can anyone tell me a simple way to print out a float 27.7 as 27.70 ?
thanks.
a = 27.7
print '%.2f' % a
27.70
If you're worrying about number of digits after point, you probably need use decimal.Decimal
Cite from the docs:
Decimal numbers can be represented exactly. In contrast, numbers like 1.1 and 2.2 do not have an exact representations in binary floating point. End users typically would not expect 1.1 + 2.2 to display as 3.3000000000000003 as it does with binary floating point.
精彩评论