如何格式化浮点数在Python可变宽度

问题描述 投票:1回答:2

我写代码,我想用户输入的愿望小数点精度。例如:

for x in numbers:
    print "{:10.*f}".format(x)

...除了其中“*”是我想放置的是其中用户提供的值的变量。我对现有的文件在溶液中搜索已被证明徒劳的。

python-3.x
2个回答
1
投票

如何print '{:10.{precision}f}'.format(x, precision=precision),其中precision是一个值别处定义?


1
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.