Python格式的负面货币 [英] Python format negative currency

查看:172
本文介绍了Python格式的负面货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆