How to make float values in Python display .00 instead of .0?

我的未来我决定 提交于 2020-06-27 17:07:23

问题


Simple question, sorry I can;t figure this out. I have some numbers that are made by float(STRING) and they are displayed as xxx.0, but I want them to end in .00 if it is indeed a whole number. How would I do this?

Thanks!

EDIT:

Python saiys that float doesn't have a cal 'format()'


回答1:


>>> '%.2f' % 2.0
'2.00'



回答2:


Also:

>>> "{0:.2f}".format(2.0)
'2.00'



回答3:


If you do not like the numbers to be rounded, you need to do little more:

>>> "%.2f"  % 1.99999
'2.00'
>>> "%.2f"  % (int(1.99999*100)/100.0)
'1.99'



回答4:


>>> "{0:.2f}".format(2)
'2.00'

For more information about the {0}.format() syntax, look here: Format String Syntax



来源:https://stackoverflow.com/questions/3368514/how-to-make-float-values-in-python-display-00-instead-of-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!