Python,想以精确格式打印float+-00.00

2024-03-29 10:33:54 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

使用'%+06.2f'适当地设置宽度和精度。使用新样式格式字符串的等价物是'{:+06.2f}'.format(n)(或者'{0:+06.2f}',如果您的Python版本需要位置组件)。

'%+06.2f'%1.1123344

+ means always use sign
0 means zero fill to full width.
6 is the total field width including sign and decimal point
.2 means 2 decimals.
f is float

用这个就可以了

x = 50.4796
a = -50.1246

print " %+2.2f" % (x)
print " %+2.2f" % (a)

应打印以下内容

+50.48
-50.12

相关问题 更多 >