与对准字符串格式货币

问题描述:

print('this is £{0:>10.2f}'.format(3.44)) 

打印出:与对准字符串格式货币

this is £  3.44 

即分开量£。有没有什么办法可以在正确的地方打印带有货币符号的印刷间隔列?例如。

this is  £3.44    

我试图print('this is {0:£>10.2f}'.format(3.44))但得到:

this is ££££££3.44 

你需要做这两个步骤,一个是货币符号和小数位,另一个是用空格右对齐:

>>> 'this is {:>10}'.format('£{:.2f}'.format(3.44)) 
'this is  £3.44' 

如果你想用逗号分隔十万,你还可以包括格式规范,

>>> 'this is {:>10}'.format('£{:,.2f}'.format(1003.44)) 
'this is £1,003.44' 
+0

谢谢乔恩 - 这就行了! –

+0

@MarkThomas没问题!仅供参考:http://*.com/help/someone-answers – jonrsharpe

你用正确的与之对齐{:> 10},如果你要左对齐,试试这个:

In[21]: print('this is £{0:<10.2f}'.format(3.44)) 
this is £3.44