使用%x.yf浮动字符串格式

问题描述 投票:2回答:1

我正在阅读使用Python的自然语言处理,我遇到过:

count, total = 3205, 9375
"accuracy for %d words: %2.4f%%" % (total, 100 * count / total)

我已经看到%.4fbut永远不会在小数点前面有一个整数。玩弄它以下所有打印相同的东西(34.1867):

print('%.4f' %(100*count/total))
print('%0.4f' %(100*count/total))
print('%1.4f' %(100*count/total))
print('%2.4f' %(100*count/total))

等等。小数前面的值是做什么的?

python python-2.7
1个回答
1
投票

第一个数字指定minimum field width,即完整输出的最小长度。

例如,要强制输出至少有6个字符,小数点后面的2表示:

'%06.2f' % (3.141592653589793,)

输出:

003.14
© www.soinside.com 2019 - 2024. All rights reserved.