在Python 3.6+中,什么是f字符串才能将float 9.9打印为字符串09.90和10输出为10.00?

jba*_*sko 0 python python-3.x f-string

hHaving看着其他三个格式问题,这里有几十每个我没有看到任何人讲如何打印浮动的答案9.909.90,并1010.00使用相同的F-字符串:

x = 9.9
print(f"{x:02.2f}")  // should print 09.90

x = 10
print(f"{x:02.2f}")  // should print 10.00
Run Code Online (Sandbox Code Playgroud)

问题是:02.2f在上面应该代替什么?

Ray*_*oal 6

你在找

print(f"{x:05.2f}")
Run Code Online (Sandbox Code Playgroud)

小数点前的数字是字段中的字符总数,而不是要出现在小数点前的数字。

文献资料


Ale*_*ine 6

f'{x:.2f}'
Run Code Online (Sandbox Code Playgroud)

将把 x 打印到小数点后两位。

f'{x:05.2f}'
Run Code Online (Sandbox Code Playgroud)

将 x 打印到小数点后两位,总共五个字符(包括小数点)