3.3 数字的格式化输出
问题
解决方案
>>> x = 1234.56789
>>> format(x, '0,.1f')
'1,234.6'>>> format(x, 'e')
'1.234568e+03'
>>> format(x, '0.2E')
'1.23E+03'>>> 'The value is {:0,.2f}'.format(x)
'The value is 1,234.57'讨论
Last updated
>>> x = 1234.56789
>>> format(x, '0,.1f')
'1,234.6'>>> format(x, 'e')
'1.234568e+03'
>>> format(x, '0.2E')
'1.23E+03'>>> 'The value is {:0,.2f}'.format(x)
'The value is 1,234.57'Last updated
>>> swap_separators = { ord('.'):',', ord(','):'.' }
>>> format(x, ',').translate(swap_separators)
'1.234,56789'