Rounding floats with f-string

使用%-format,我可以在一个字符串中指定十进制大小写的数目:

x = 3.14159265
print('pi = %0.2f' %x)

这会给我:

pi = 3.14

在 Python 3.6中有没有使用 f-string 来实现这一点的方法?

106016 次浏览

这个怎么样

x = 3.14159265
print(f'pi = {x:.2f}')

F 弦的文档