Opened 12 years ago
Closed 12 years ago
#19282 closed Cleanup/optimization (fixed)
intcomma for decimal no longer works as expected
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)
Change History (8)
comment:1 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 12 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 12 years ago
Has patch: | set |
---|
comment:5 by , 12 years ago
Keywords: | sprint-django-ar added |
---|
comment:6 by , 12 years ago
Keywords: | sprints-django-ar added; sprint-django-ar removed |
---|
comment:7 by , 12 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:8 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
This is a side effect of the fix for #6392.
It makes sense to format decimals like floats there.