我有一个变量n
,我想打印n位小数点。
import math
n = 5
print(f"{math.pi:.nf}")
ValueError: Format specifier missing precision
这不管用,但怎么做呢?
发布于 2021-01-28 16:03:58
格式字符串可以嵌套中的字段
>>> print(f"{math.pi:.{n}f}")
3.14159
发布于 2021-01-28 16:04:08
对于之前的3.6版本,可以使用.format()
。
print('{:.{}}'.format(math.pi, n)))
https://stackoverflow.com/questions/65940742
复制相似问题