我正在制作number formatter in this question的通用版本,并且正在考虑在f字符串的数字格式中使用变量来控制数字的数量。
def number_format(x: float,d: int):
assert d >= 0,f"{d} is a negative number and won't work"
x_size: int = 0 if not x else int(log(abs(x)))
n: int = max(d - x_size,0)
return f"{x:,.nf}"
这将无法正常工作。如何用可变数字位数格式化数字?