Opened 12 years ago

Closed 12 years ago

#19282 closed Cleanup/optimization (fixed)

intcomma for decimal no longer works as expected

Reported by: judy Owned by: hernantz
Component: contrib.humanize Version: 1.4
Severity: Normal Keywords: intcomma, humanize, decimal, sprints-django-ar
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: 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)


Change History (8)

comment:1 by Aymeric Augustin, 12 years ago

Triage Stage: UnreviewedAccepted

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

It makes sense to format decimals like floats there.

comment:2 by hernantz, 12 years ago

Owner: changed from nobody to hernantz
Status: newassigned

comment:4 by hernantz, 12 years ago

Has patch: set

comment:5 by hernantz, 12 years ago

Keywords: sprint-django-ar added

comment:6 by hernantz, 12 years ago

Keywords: sprints-django-ar added; sprint-django-ar removed

comment:7 by Claude Paroz, 12 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In 7e6ad76b24a003bfb4ced8d9b3b22a46c1590e02:

Fixed #19282 -- Restored ability to pass Decimals to intcomma filter

Note: See TracTickets for help on using tickets.
Back to Top