繁体   English   中英

python中的格式数字,带有前导零和固定的小数位

[英]Format number in python with leading zeros and fixed decimal places

我正在尝试将数字格式化为python中的字符串。 我希望将0.1423的结果转换为000.14 我试过了

num = 0.1423
print '%0.2f' %num

但这只会导致0.14 我似乎无法得到前导零。

干杯,詹姆斯

num = 0.1423
print '%06.2f' %num

六个表示字段的总宽度,包括小数点。 零表示包括前导零,数字2表示精度。

还必须提供字段宽度以获取所需的前导零个数:

print "%06.2f" % num

输出:

000.14

使用str.format

 print "{:06.2f}".format(num)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM