If we're printing a dollar amount, we usually want to always display two decimal digits.
cost1, cost2 = 123.456890123456789, 357.000
print '{c1:.2f} {c2:.2f}'.format(c1=cost1, c2=cost2)
shows
123.46 357.00
But on other occasions we'd like to print the fractions only if they matter. If the two numbers above were volume, for instance, we may prefer to display
123.45 gal. 357 gal.
Can this be obtained directly with format
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…