views:

350

answers:

2

Why?

C:\path\>manage.py shell
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.    

>>> import locale
>>> locale.getlocale()
('Spanish_Colombia', '1252')
>>> locale.currency( 1885, grouping=True )
'$ 1.885,00'
>>> locale.currency( -1885, grouping=True )
'($ 1.885,00)'

Can´t return $ -1.885,00?

+2  A: 

In your locale, surrounding a number with brackets indicates it's a negative number. Check this in Control Panel/Regional and Language settings, Python's probably picking it up from there.

Ian Kemp
>>> locale.setlocale( locale.LC_ALL, '' )'English_United States.1252'>>> locale.currency( -1885, grouping=True )'($1,885.00)' - so, its not only my locale. why?
panchicore
"Python's probably picking it up from there". Not quite right. That defines the locale format. Python always find the Locale format provided by the OS. It's an OS question -- what locale is defined by the OS.
S.Lott
+2  A: 

Parentheses around a number to indicate it's a debit (i.e. negative), not a credit (positive), are a common convention in accounting (I guess because the parentheses are more visible than a small minus/dash in front, and it's crucial to distinguish debits from credits;-).

So, it's not surprising that many locales express that convention as the "proper way" to format negative numbers. If you want to use some parts of your locale's conventions, such as the $ sign and the commas, but not others, such as the parentheses, you'll have to use abs(yournumber) instead of just yournumber as the input to locale.currency, then, if yournumber < 0, do a little string manipulation to find the first digit and form a new string with the dash in front of it (or other string manipulations, depending on your desired way to express negative amounts -- e.g. the sign "minus" [dash] could go before the currency symbol, or at the right of the whole string).

Why do you think that whoever's going to be reading that output wants some but not all of the locale's conventions to apply to it, by the way?

Alex Martelli