Opened 2 years ago

Closed 2 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 Changed 2 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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

It makes sense to format decimals like floats there.

comment:2 Changed 2 years ago by hernantz

  • Owner changed from nobody to hernantz
  • Status changed from new to assigned

comment:4 Changed 2 years ago by hernantz

  • Has patch set

comment:5 Changed 2 years ago by hernantz

  • Keywords sprint-django-ar added

comment:6 Changed 2 years ago by hernantz

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

comment:7 Changed 2 years ago by claudep

  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 2 years ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 7e6ad76b24a003bfb4ced8d9b3b22a46c1590e02:

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

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