如何在Python中格式化如1'123'456,00

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

如何将

",.2f"
格式化为
"'.2f"
或者我应该如何在使用 format() 进行格式化时将逗号分隔符更改为
'

format (123456789, ',.2f') => 1,234,567.89

format (123456789, '???.2f') => 1'234'567.89
python format
3个回答
0
投票

尝试:

s = f"{123456789 / 100:,}".replace(",", "'")
print(s)

打印:

1'234'567.89

0
投票

您可以通过结合使用字符串操作和格式化来实现所需的结果。

这是一个简单的例子:

number = 123456789
formatted_number = "{:,.2f}".format(number) 

formatted_number_with_quotes = formatted_number.replace(",", "'")

print(formatted_number_with_quotes)

输出:

123'456'789.00

0
投票

您可以采取的解决方法是执行正常格式方法并使用replace() 将逗号(,) 替换为(')。

format (123456789, ',.2f').replace(',', "'") => 123'456'789.00
© www.soinside.com 2019 - 2024. All rights reserved.