Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字符串格式浮动为%x.yf_Python_Python 2.7 - Fatal编程技术网

Python 字符串格式浮动为%x.yf

Python 字符串格式浮动为%x.yf,python,python-2.7,Python,Python 2.7,我正在阅读Python的自然语言处理,我发现: count, total = 3205, 9375 "accuracy for %d words: %2.4f%%" % (total, 100 * count / total) 我见过%.4f,但从未见过小数点前有整数。玩弄它下面所有的都打印相同的东西(34.1867): 等等。小数点前面的值是做什么的 第一个数字指定输出的长度,即完整输出的最小长度 例如,要强制输出至少包含6个字符,小数点后有2个字符,请执行以下操作: '%06.2f' %

我正在阅读Python的自然语言处理,我发现:

count, total = 3205, 9375
"accuracy for %d words: %2.4f%%" % (total, 100 * count / total)
我见过
%.4f
,但从未见过小数点前有整数。玩弄它下面所有的都打印相同的东西(
34.1867
):


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

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

例如,要强制输出至少包含6个字符,小数点后有2个字符,请执行以下操作:

'%06.2f' % (3.141592653589793,)
输出:

003.14

这意味着在小数点前至少有2个数字,34已经有了-请尝试使用小于10的值,或者使模板中的数字大于2。请参阅文档:@jornsharpe似乎填充基于总字符,即小数点前后的数字以及小数点本身。因此
%7.4f
仍然生成
34.1867
,但
%8.4f
生成'34.1867'。对于文档,它是整个数字的“最小字段宽度”。
003.14