当前位置: 动力学知识库 > 问答 > 编程问答 >

Python: how to display exact decimal value

问题描述:

I have a dictionary like

>> dic = {'yearly': 79.00, 'monthly': 59.00}

when the dic is printed the, it removes the last zero from the decimal number.

>> print dic

>> {'monthly': 59.0, 'yearly': 79.0}

How to get the original value as 59.00 not as 59.0 ?

网友答案:

when you print a number, you could do

x = 5
print '%.2f' % x

where the 2 specifies u want 2 decimal place

alternatively, the more updated/versatile version is

print '{:.2f}'.format(x)

if you really want your dict object to print nicely, you could create a custom class that specifies the __str__ function to print the dict however you want to print it

分享给朋友:
您可能感兴趣的文章:
随机阅读: