Python format negative currency
Asked Answered
W

3

5

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

Result with Negative Number:

-496.06 ($496.06)

I need a minus sign NOT parenthesis

How do I get rid of the parenthesis and get minus signs?

Wolk answered 15/5, 2015 at 11:39 Comment(4)
Forgot to add this line of code: locale.setlocale( locale.LC_ALL, 'English_United States.1252' )Wolk
Why? It's pretty common to use parenthesis to indicate negative currency/Sami
Also forgot to add that this is Windows 7 x64Wolk
As to why? I am storing data in a pyQT Table and I have to convert it to place it in the Table and convert it coming out. Being a novice I couldn't find a way to get around the errors when doing the string to Decimal conversions. While waiting I ended up writing my own toCurrency function that accepts a QString, String or Decimal.Wolk
L
3

Looks like you can use the _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 could use string formatting.

Loraleeloralie answered 15/5, 2015 at 13:41 Comment(0)
A
2

Here's a simple function based answer in Python 3 that requires you to provide a number and uses .format with the amount fixed to two decimal points:

def format_dollar_amount(amount):
    formatted_absolute_amount = '${:,.2f}'.format(abs(amount))
    if round(amount, 2) < 0:
        return f'-{formatted_absolute_amount}'
    return formatted_absolute_amount

Here are some example outputs:

>>> format_dollar_amount(-1)
'-$1.00'

>>> format_dollar_amount(0)
'$0.00'

>>> format_dollar_amount(123)
'$123.00'

>>> format_dollar_amount(-0.00001)
'$0.00'

>>> format_dollar_amount(-15.2)
'-$15.20'

>>> format_dollar_amount(123456789)
'$123,456,789.00'
Alpine answered 5/10, 2022 at 16:57 Comment(0)
F
0

This may not be the comprehensive approach you seek, but if you use the locale en_US.UTF-8, you can have a deterministic approach with the the negative sign -:

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
Finsteraarhorn answered 15/5, 2015 at 12:23 Comment(3)
I get this error: Traceback (most recent call last): File "D:/Python Projects/Budget/Budget.pyw", line 168, in <module> locale.setlocale(locale.LC_ALL, b'en_US.UTF-8') File "C:\Python27\lib\locale.py", line 579, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale settingWolk
@user3279899 what version of python?Finsteraarhorn
Python 2.7 is the version I am runningWolk

© 2022 - 2024 — McMap. All rights reserved.