[英]In Python 3.6+ what is the f-string to print float 9.9 as string 09.90 and 10 as 10.00?
hHaving looked at three other formatting questions here with tens of answers each I don't see anyone telling how to print a float 9.9
as 09.90
and 10
as 10.00
using the same f-string: h在这里看了其他三个格式化问题,每个问题都有数十个答案,我看不到有人告诉我如何使用相同的f字符串将float
9.9
打印为09.90
,将10
打印为10.00
。
x = 9.9
print(f"{x:02.2f}") // should print 09.90
x = 10
print(f"{x:02.2f}") // should print 10.00
The question is what should be in place of :02.2f
in the above? 问题是,在上面的
:02.2f
处应该代替什么?
You are looking for 你在找
print(f"{x:05.2f}")
The number before the radix point is the total number of characters in the field, not the number to appear before the radix point. 小数点前的数字是字段中的字符总数 ,而不是出现在小数点前的数字。
f'{x:.2f}'
will print x to two decimal places. 将x打印到两位小数。
f'{x:05.2f}'
will print x to two decimal places, with five total characters (including the decimal point) 将x打印到两位小数,总共五个字符(包括小数点)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.