问题
你需要将数字格式化后输出,并控制数字的位数、对齐、千位分隔符和其他的细节。
解决方案
格式化输出单个数字的时候,可以使用内置的 format() 函数,比如:
>>> x = 1234.56789
>>> # Two decimal places of accuracy
>>> format(x, '0.2f')
'1234.57'
>>> # Right justified in 10 chars, one-digit accuracy
>>> format(x, '>10.1f')
' 1234.6'
>>> # Left justified
>>> format(x, '<10