如何将
",.2f"
格式化为 "'.2f"
或者我应该如何在使用 format() 进行格式化时将逗号分隔符更改为 '
format (123456789, ',.2f') => 1,234,567.89
到
format (123456789, '???.2f') => 1'234'567.89
尝试:
s = f"{123456789 / 100:,}".replace(",", "'")
print(s)
打印:
1'234'567.89
您可以通过结合使用字符串操作和格式化来实现所需的结果。
这是一个简单的例子:
number = 123456789
formatted_number = "{:,.2f}".format(number)
formatted_number_with_quotes = formatted_number.replace(",", "'")
print(formatted_number_with_quotes)
输出:
123'456'789.00
您可以采取的解决方法是执行正常格式方法并使用replace() 将逗号(,) 替换为(')。
format (123456789, ',.2f').replace(',', "'") => 123'456'789.00