Ticket #9037: humanize_intdot.diff

File humanize_intdot.diff, 2.4 KB (added by Wiliam Alves de Souza, 16 years ago)
  • django/contrib/humanize/templatetags/humanize.py

     
    3737intcomma.is_safe = True
    3838register.filter(intcomma)
    3939
     40def intdot(value):
     41    """
     42    Converts an integer to a string containing dots every three digits.
     43    For example, 3000 becomes '3.000' and 45000 becomes '45.000'.
     44    """
     45    orig = force_unicode(value)
     46    new = re.sub("^(-?\d+)(\d{3})", '\g<1>.\g<2>', orig)
     47    if orig == new:
     48        return new
     49    else:
     50        return intdot(new)
     51intdot.is_safe = True
     52register.filter(intdot)
     53
    4054def intword(value):
    4155    """
    4256    Converts a large integer to a friendly text representation. Works best for
  • tests/regressiontests/humanize/tests.py

     
    3737
    3838        self.humanize_tester(test_list, result_list, 'intcomma')
    3939
     40    def test_intdot(self):
     41        test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
     42                     '100', '1000', '10123', '10311', '1000000', '1234567.1234567')
     43        result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567.25',
     44                       '100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567.1234567')
     45
     46        self.humanize_tester(test_list, result_list, 'intdot')
     47
    4048    def test_intword(self):
    4149        test_list = ('100', '1000000', '1200000', '1290000',
    4250                     '1000000000','2000000000','6000000000000')
  • docs/ref/contrib/humanize.txt

     
    4242
    4343You can pass in either an integer or a string representation of an integer.
    4444
     45intdot
     46------
     47
     48Converts an integer to a string containing dots every three digits.
     49
     50Examples:
     51
     52    * ``4500`` becomes ``'4.500'``.
     53    * ``45000`` becomes ``'45.000'``.
     54    * ``450000`` becomes ``'450.000'``.
     55    * ``4500000`` becomes ``'4.500.000'``.
     56
     57You can pass in either an integer or a string representation of an integer.
     58
    4559intword
    4660-------
    4761
Back to Top