Ticket #17662: django_percentage.diff

File django_percentage.diff, 3.5 KB (added by eire1130, 3 years ago)

percentage filter

  • django/contrib/humanize/templatetags/humanize.py

    diff -r 385d1f81c5c7 -r 975534756783 django/contrib/humanize/templatetags/humanize.py
    a b  
    222222            return ungettext(
    223223                u'an hour from now', u'%(count)s hours from now', count
    224224            ) % {'count': count}
     225           
     226@register.filter
     227def percentage(value, precision=2):
     228    """
     229    Returns Percentage representation of number types.
     230    Takes an optional integer argument for decimal point accuracy.
     231    Upon failure returns value.
     232    """
     233    try:
     234        precision = int(precision)
     235        value = float(value)
     236    except (TypeError, ValueError):
     237        return value
     238    places = ':.{precision}%'.format(precision=precision)
     239    perc_holder =  '{' + places + '}'
     240    return perc_holder.format(value)
  • django/contrib/humanize/tests.py

    diff -r 385d1f81c5c7 -r 975534756783 django/contrib/humanize/tests.py
    a b  
    109109                       someday_result, u"I'm not a date value", None)
    110110        self.humanize_tester(test_list, result_list, 'naturalday')
    111111
     112    def test_percentage(self):
     113        from fractions import Fraction
     114        from decimal import Decimal
     115        fl_value = 1.5
     116        int_value = 1
     117        int_fraction = Fraction(1)/2
     118        int_decimal = Decimal(1) / Decimal(2)
     119        notnumber = u"I'm not a number"
     120
     121        test_list = (fl_value, int_value, int_fraction, int_decimal, notnumber, None)
     122        result_list = (u'150.00%', u'100.00%', u'50.00%',u'50.00%',
     123                        notnumber, None)
     124        self.humanize_tester(test_list, result_list, 'percentage')
     125        result_list = (u'150.000%', u'100.000%', u'50.000%',u'50.000%',
     126                        notnumber, None)
     127        self.humanize_tester(test_list, result_list, 'percentage:3')
     128        result_list = (fl_value, int_value, int_fraction,int_decimal,
     129                        notnumber, None)
     130        self.humanize_tester(test_list, result_list, "percentage:'a'")
     131
    112132    def test_naturalday_tz(self):
    113133        from django.contrib.humanize.templatetags.humanize import naturalday
    114134
     
    200220        finally:
    201221            humanize.datetime = orig_humanize_datetime
    202222            timesince.datetime = orig_timesince_datetime
     223
     224
  • docs/ref/contrib/humanize.txt

    diff -r 385d1f81c5c7 -r 975534756783 docs/ref/contrib/humanize.txt
    a b  
    137137* ``3`` becomes ``3rd``.
    138138
    139139You can pass in either an integer or a string representation of an integer.
     140
     141.. templatefilter:: percentage
     142
     143percentage
     144----------
     145   
     146Converts a number to a percentage.
     147Takes an optional integer argument for decimal point accuracy.
     148
     149Examples:
     150
     151* ``1.5`` becomes ``150.00%``.
     152* ``Fraction(1)/2`` becomes ``50.00%``.
     153* ``Decimal(1) / Decimal(2)`` becomes ``50.00%``.
     154* ``not a number`` returns ``not a number``.
     155
     156You can pass in either an integer or a string representation of a number.
     157**Argument:** Decimal precision can be set utilizing the precision optional argument.
     158Any number type can be passed, but the function will convert to an integer,
     159following Python's conversion rules.
     160
     161For example::
     162
     163    {{ value|percentage:3}}
     164   
     165* ``1.5`` becomes ``150.000%``.
     166
     167
     168Default precision is set to 2.
     169 No newline at end of file
Back to Top