Ticket #3017: commaseparate.2.patch

File commaseparate.2.patch, 2.1 KB (added by SmileyChris, 8 years ago)
  • django/contrib/humanize/templatetags/humanize.py

     
    1818    return '%d%s' % (value, t[value % 10])
    1919register.filter(ordinal)
    2020
    21 def intcomma(value):
     21def commaseparate(value):
    2222    """
    23     Converts an integer to a string containing commas every three digits.
     23    Converts an number to a string containing commas every three digits.
    2424    For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
    2525    """
    2626    orig = str(value)
    27     new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', str(value))
     27    new = re.sub("^(-?\d+)(\d{3})((?:\.\d+)?)", r'\1,\2\3', str(value))
    2828    if orig == new:
    2929        return new
    3030    else:
    31         return intcomma(new)
     31        return commaseparate(new)
     32register.filter(commaseparate)
     33
     34# Backwards compatibility (commaseparate used to be called intcomma).
     35intcomma = commaseparate
    3236register.filter(intcomma)
    3337
    3438def intword(value):
  • docs/add_ons.txt

     
    8787
    8888You can pass in either an integer or a string representation of an integer.
    8989
    90 intcomma
    91 --------
     90commaseparate
     91-------------
    9292
    93 Converts an integer to a string containing commas every three digits.
     93**New in Django development version**: ``commaseparate`` (which was previously
     94called ``intcomma``) now works with both integers and floating point numbers.
    9495
     96Converts a number to a string containing commas every three digits (before any
     97decimal place).
     98
    9599Examples:
    96100
    97101    * ``4500`` becomes ``'4,500'``.
    98102    * ``45000`` becomes ``'45,000'``.
    99103    * ``450000`` becomes ``'450,000'``.
    100104    * ``4500000`` becomes ``'4,500,000'``.
     105    * ``4500.0001`` becomes ``'4,500.0001'``.
    101106
    102 You can pass in either an integer or a string representation of an integer.
     107You can pass in either an integer, a float or a string representation of an
     108integer or float.
    103109
    104110intword
    105111-------
Back to Top