简体   繁体   English

格式化浮点元组

[英]Formatting a tuple of floats

How can I format a tuple of float to .3f ? 如何将float的元组格式化为.3f

In [1]: pi=3.14159265359

In [2]: print "{:.3f}".format(pi)
3.142

In [3]: e=2.71828182

In [4]: z=(pi,e)

In [5]: print z
(3.14159265359, 2.71828182)

You format each float value in the tuple: 您可以元组中格式化每个浮点值:

print ' '.join(format(f, '.3f') for f in z)

I used the format() function here to achieve the same formatting for individual floating point values. 我在这里使用format()函数来为单个浮点值实现相同的格式。

Output: 输出:

>>> pi=3.14159265359
>>> e=2.71828182
>>> z=(pi,e)
>>> print ' '.join(format(f, '.3f') for f in z)
3.142 2.718

Adjust the join string as needed. 根据需要调整连接字符串。

你可以用

print '{:.3f} {:.3f}'.format(*z)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM