python - 带对齐的字符串格式货币

标签 python python-3.x

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'

关于python - 带对齐的字符串格式货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42658771/

相关文章:

python-3.x - 为什么 pandas.where() 返回 'None'

python - 从 python 脚本将日志写入/var/log 的最佳实践?

python-3.x - 获取旧问题的数量以及存储库最活跃成员的表(登录和提交数量)

python - 谜语: The Square Puzzle

python - 类型错误 : first argument must be callable or None - error is not coming first time but coming later

python - PyTorch C++扩展:如何索引张量并更新它?

python - PyPDF2 复制后返回空白 PDF

Python - 修改反向引用。可以吗?

python - 暂停系统功能,直到我的 python 脚本完成

python-3.x - 数据框连接列