我写代码,我想用户输入的愿望小数点精度。例如:
for x in numbers:
print "{:10.*f}".format(x)
...除了其中“*
”是我想放置的是其中用户提供的值的变量。我对现有的文件在溶液中搜索已被证明徒劳的。
如何print '{:10.{precision}f}'.format(x, precision=precision)
,其中precision
是一个值别处定义?
Python中的单行
number = 10.123456789
print ('{:10.{precision}f}'.format(number, precision=int(input("Enter the precision"))))
Output :
>>> print ('{:10.{precision}f}'.format(number, precision=int(input("Enter the precision\n"))))
Enter the precision
5
10.12346