gpt4 book ai didi

Python 3.5 字符串格式 : How to add a thousands-separator and also right justify?

转载 作者:行者123 更新时间:2023-12-03 18:54:14 24 4
gpt4 key购买 nike

我将如何获得等于的字符串:

'    100,000.23'

鉴于我有变量
num = 100000.23

我可以正确地证明:
num = 100000.23
'{:>10.2f}'.format(num)

我可以用以下方法分开数千:
num = 100000.23
'{:,}'.format(num)

但是我怎样才能同时做到这两点呢?

最佳答案

通过在对齐说明之后添加逗号来组合两者:

>>> '{:>12,.2f}'.format(num)
' 100,000.23'

说明
供引用: Format Specification Mini-language
{:       >      12         ,               .2         f      }
^^^ ^^ ^^^ ^^^ ^^^
align width grouping_option precision type

关于Python 3.5 字符串格式 : How to add a thousands-separator and also right justify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com