如何摆脱python中的小数点?

时间:2017-10-19 17:42:39

标签: python floating-point decimal

例如,我想将0.5转换为05,0.25到025,我怎么能这样做?

3 个答案:

答案 0 :(得分:5)

您可以简单地将浮点数转换为字符串,然后从字符串

中删除.
number = 0.5
print str(number).replace(".", "")

答案 1 :(得分:0)

如果你想获得一个字符串,你可以使用它:

str(0.5).replace('.','')

答案 2 :(得分:0)

replace()函数用其他内容替换字符串中的字符:
e.g。

print('>>> 12345'.replace('1','0'))

会打印:

>>> 02345

替换功能有两个参数,即:
 replace(what you are replacing, what it is changed to)

所以你的回答是:
 print(str(0.5).replace('.',''))