Python format negative currency

I haven't found anything as to 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

      

Result with negative number:

-496.06 ($496.06)

      

I need a minus sign NOT parentheses

How do I get rid of parentheses and get minus signs?

+3


source to share


2 answers


It looks like you can use a _override_localeconv

dict (which is a bit hackish).

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

      



or you can use string formatting .

+2


source


It may not be the comprehensive approach you are looking for, but if you are using a locale en_US.UTF-8

, you can have a negative deterministic approach -

:



import locale
locale.setlocale(locale.LC_ALL, b'en_US.UTF-8')

amount = locale.currency(-350, grouping=True)
print(amount) # -$350.00

amount = locale.currency(-350, grouping=True).replace('$', '')
print(amount) # -350.00

      

0


source







All Articles