[Python-bugs-list] [Bug #129417] Negative numbers are grouped incorrectly
noreply@sourceforge.net
noreply@sourceforge.net
Sun, 21 Jan 2001 10:53:46 -0800
Bug #129417, was updated on 2001-Jan-19 13:00
Here is a current snapshot of the bug.
Project: Python
Category: Python Library
Status: Closed
Resolution: Fixed
Bug Group: None
Priority: 5
Submitted by: nobody
Assigned to : nobody
Summary: Negative numbers are grouped incorrectly
Details: >>> import locale
>>> locale.setlocale(locale.LC_ALL,'')
'en'
>>> print locale.format("%.2f", -100, 1)
-,100.00
Linux 2.4.0 on Mandrake Linux 7.2, PIII-866.
Hmmm. The problem is in locale.py. The
(very simplistic) algorithm to add grouping
characters only works for positive numbers.
It can be re-written as:
def format(f,val,grouping=0):
"""Formats a value in the same way that the % formatting would use,
but takes the current locale into account.
Grouping is applied if the third parameter is true."""
result = f % val
fields = string.split(result, ".")
if grouping:
fields[0]=_group(fields[0])
if len(fields)==2:
res = fields[0]+localeconv()['decimal_point']+fields[1]
elif len(fields)==1:
res = fields[0]
else:
raise Error, "Too many decimal points in result string"
if val < 0:
return '-'+res
else:
return res
-Kevin Jacobs
(jacobs@theopalgroup.com)
Follow-Ups:
Date: 2001-Jan-21 10:53
By: loewis
Comment:
Suggested fix applied in 1.12 of locale.py.
-------------------------------------------------------
Date: 2001-Jan-19 16:15
By: nobody
Comment:
Oops. That will teach me to cut-and-paste
without looking.
result = f % val
should be
result = f % abs(val)
-Kevin
-------------------------------------------------------
Date: 2001-Jan-19 15:01
By: gvanrossum
Comment:
Martin, didn't you write that code originally? If you agree with the fix,
just check it in.
-------------------------------------------------------
For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=129417&group_id=5470