Ticket #17662: django_percentage_take_two.diff

File django_percentage_take_two.diff, 4.0 KB (added by James Reynolds, 13 years ago)

percentage with diff

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

    diff -r 385d1f81c5c7 -r 9fed087a8a42 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)*100
     236    except (TypeError, ValueError):
     237        return value
     238    value = round(value, precision)
     239    return number_format(value,decimal_pos=precision)+u'%'
     240    #places = ':.{precision}%'.format(precision=precision)
     241    #perc_holder =  '{' + places + '}'
     242    #return perc_holder.format(value)
  • django/contrib/humanize/tests.py

    diff -r 385d1f81c5c7 -r 9fed087a8a42 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 = 10
     117        int_fraction = Fraction(1)/2
     118        int_decimal = Decimal(1) / Decimal(2)
     119        notnumber = u"I'm not a number"
     120        long_float = .123456789
     121
     122        test_list = (fl_value, int_value, int_fraction, int_decimal, long_float,notnumber, None)
     123        result_list = (u'150.00%', u'1000.00%', u'50.00%',u'50.00%',u'12.35%',
     124                        notnumber, None)
     125        self.humanize_tester(test_list, result_list, 'percentage')
     126        result_list = (u'150.000%', u'1000.000%', u'50.000%',u'50.000%',u'12.346%',
     127                        notnumber, None)
     128        self.humanize_tester(test_list, result_list, 'percentage:3')
     129        result_list = (fl_value, int_value, int_fraction,int_decimal,long_float,
     130                        notnumber, None)
     131        self.humanize_tester(test_list, result_list, "percentage:'a'")
     132        with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
     133            result_list = (u'150.000%', u'1,000.000%', u'50.000%',u'50.000%',u'12.346%',
     134                            notnumber, None)
     135            self.humanize_tester(test_list, result_list, 'percentage:3')
     136
    112137    def test_naturalday_tz(self):
    113138        from django.contrib.humanize.templatetags.humanize import naturalday
    114139
     
    200225        finally:
    201226            humanize.datetime = orig_humanize_datetime
    202227            timesince.datetime = orig_timesince_datetime
     228
     229
  • docs/ref/contrib/humanize.txt

    diff -r 385d1f81c5c7 -r 9fed087a8a42 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