使用 babel 格式化货币而不使用 Unicode?

问题描述 投票:0回答:1

使用 Babel 我得到这个:

>>> babel.numbers.format_currency(1099.98, 'CHF', locale='fr_CH')
'1\u202f099,98\xa0CHF'

我想要这种格式:

"CHF 1'099.98"

是否可以用babel指定自定义格式?

我目前丑陋的解决方法是:

>>> num = 1099.998
>>> s = f"CHF {num:,.2f}".replace(',',"'")
>>> s
"CHF 1'100.00"

但它也舍入了我的数字:(

python format numbers python-babel
1个回答
0
投票

问题是你的

.2f
(四舍五入到小数点后两位)。如果您想要更高的精度,请使用更大的数字(例如
.10f
),或者如果您不希望任何舍入,请直接传递您的数字。

f"CHF {num:,f}".replace(",","'")
© www.soinside.com 2019 - 2024. All rights reserved.