Opened 7 years ago

Closed 7 years ago

intcomma for decimal no longer works as expected

Reported by: Owned by: judy hernantz contrib.humanize 1.4 Normal intcomma, humanize, decimal, sprints-django-ar Ready for checkin yes no no no no no

Description

intcomma used to work for decimals, but this has been broken somewhere between django 1.2.3 and django 1.4...
e.g.

```>>> from decimal import Decimal
>>> i = Decimal("1233566.22")
>>> intcomma(i)
u'1,233,566'
```

I guess technically the name is intcomma, so maybe it was officially only supposed to work for integers; however it appears that intcomma specifically supports floats:

```>>> i = 12356333.44
>>> intcomma(i)
u'12,356,333.44'
```

for reference ---
Old humanize.py:

```def intcomma(value):
"""
Converts an integer to a string containing commas every three digits.
For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
"""
orig = force_unicode(value)
new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
if orig == new:
return new
else:
return intcomma(new)
```

New humanize.py (note that it specifically mentions float.. perhaps we could specifically mention decimal as well??):

```def intcomma(value, use_l10n=True):
"""
Converts an integer to a string containing commas every three digits.
For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
"""
if settings.USE_L10N and use_l10n:
try:
if not isinstance(value, float):
value = int(value)
except (TypeError, ValueError):
return intcomma(value, False)
else:
return number_format(value, force_grouping=True)
orig = force_unicode(value)
new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
if orig == new:
return new
else:
return intcomma(new, use_l10n)

```

comment:1 Changed 7 years ago by Aymeric Augustin

Triage Stage: Unreviewed → Accepted

This is a side effect of the fix for #6392.

It makes sense to format decimals like floats there.

comment:2 Changed 7 years ago by hernantz

Owner: changed from nobody to hernantz new → assigned

comment:4 Changed 7 years ago by hernantz

Has patch: set