这是我的代码:
print str(float(1/3))+'%'
它显示:
0.0%
但我想得到 33%
我能做什么?
最佳答案
format
支持百分比floating point precision type :
>>> print "{0:.0%}".format(1./3)
33%
如果不想整数除法,可以从__future__
导入Python3的除法。 :
>>> from __future__ import division
>>> 1 / 3
0.3333333333333333
# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format(1/3 * 100)
33%
# Or even shorter using the format mini language:
>>> print "{:.0%}".format(1/3)
33%
https://stackoverflow.com/questions/5306756/