Python格式的负面货币 [英] Python format negative currency
问题描述
I haven't found anything that addresses how to format negative currency, so far, and it is driving me crazy.
from decimal import *
import re
import sys
import os
import locale
locale.setlocale( locale.LC_ALL, 'English_United States.1252' )
# cBalance is a running balance of type Decimal
fBalance = locale.currency( cBalance, grouping=True )
print cBalance, fBalance
我还没有发现任何解决如何格式化负面货币的方法。 > from decimal import *
import re
import sys
import os
import locale
locale.setlocale(locale.LC_ALL,'英语美国1252')
#cBalance是Decimal
的运行余额fBalance = locale.currency(cBalance,grouping = True)
print cBalance,fBalance
Result with Negative Number:
结果为负数:
-496.06 ($496.06)
I need a minus sign NOT parenthesis
我需要一个减号 NOT 括号
How do I get rid of the parenthesis and get minus signs?
如何去除括号并得到减号?
推荐答案
看起来你可以使用 _override_localeconv
dict(这有点不妥)。 $ p> 导入区域设置
cBalance = -496.06
locale.setlocale(locale.LC_ALL,'English_United
locale._override_localeconv = {'n_sign_posn':1}
fBalance = locale.currency(cBalance,grouping = True)
print cBalance,fBalance
import locale
cBalance = -496.06
locale.setlocale( locale.LC_ALL, 'English_United States.1252')
locale._override_localeconv = {'n_sign_posn':1}
fBalance = locale.currency(cBalance, grouping=True)
print cBalance, fBalance
或者您可以使用字符串格式化。
这篇关于Python格式的负面货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!