diff -r 385d1f81c5c7 -r 9fed087a8a42 django/contrib/humanize/templatetags/humanize.py --- a/django/contrib/humanize/templatetags/humanize.py Mon Feb 06 02:00:19 2012 +0000 +++ b/django/contrib/humanize/templatetags/humanize.py Wed Feb 08 16:36:14 2012 -0500 @@ -222,3 +222,21 @@ return ungettext( u'an hour from now', u'%(count)s hours from now', count ) % {'count': count} + +@register.filter +def percentage(value, precision=2): + """ + Returns Percentage representation of number types. + Takes an optional integer argument for decimal point accuracy. + Upon failure returns value. + """ + try: + precision = int(precision) + value = float(value)*100 + except (TypeError, ValueError): + return value + value = round(value, precision) + return number_format(value,decimal_pos=precision)+u'%' + #places = ':.{precision}%'.format(precision=precision) + #perc_holder = '{' + places + '}' + #return perc_holder.format(value) diff -r 385d1f81c5c7 -r 9fed087a8a42 django/contrib/humanize/tests.py --- a/django/contrib/humanize/tests.py Mon Feb 06 02:00:19 2012 +0000 +++ b/django/contrib/humanize/tests.py Wed Feb 08 16:36:14 2012 -0500 @@ -109,6 +109,31 @@ someday_result, u"I'm not a date value", None) self.humanize_tester(test_list, result_list, 'naturalday') + def test_percentage(self): + from fractions import Fraction + from decimal import Decimal + fl_value = 1.5 + int_value = 10 + int_fraction = Fraction(1)/2 + int_decimal = Decimal(1) / Decimal(2) + notnumber = u"I'm not a number" + long_float = .123456789 + + test_list = (fl_value, int_value, int_fraction, int_decimal, long_float,notnumber, None) + result_list = (u'150.00%', u'1000.00%', u'50.00%',u'50.00%',u'12.35%', + notnumber, None) + self.humanize_tester(test_list, result_list, 'percentage') + result_list = (u'150.000%', u'1000.000%', u'50.000%',u'50.000%',u'12.346%', + notnumber, None) + self.humanize_tester(test_list, result_list, 'percentage:3') + result_list = (fl_value, int_value, int_fraction,int_decimal,long_float, + notnumber, None) + self.humanize_tester(test_list, result_list, "percentage:'a'") + with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): + result_list = (u'150.000%', u'1,000.000%', u'50.000%',u'50.000%',u'12.346%', + notnumber, None) + self.humanize_tester(test_list, result_list, 'percentage:3') + def test_naturalday_tz(self): from django.contrib.humanize.templatetags.humanize import naturalday @@ -200,3 +225,5 @@ finally: humanize.datetime = orig_humanize_datetime timesince.datetime = orig_timesince_datetime + + diff -r 385d1f81c5c7 -r 9fed087a8a42 docs/ref/contrib/humanize.txt --- a/docs/ref/contrib/humanize.txt Mon Feb 06 02:00:19 2012 +0000 +++ b/docs/ref/contrib/humanize.txt Wed Feb 08 16:36:14 2012 -0500 @@ -137,3 +137,32 @@ * ``3`` becomes ``3rd``. You can pass in either an integer or a string representation of an integer. + +.. templatefilter:: percentage + +percentage +---------- + +Converts a number to a percentage. +Takes an optional integer argument for decimal point accuracy. + +Examples: + +* ``1.5`` becomes ``150.00%``. +* ``Fraction(1)/2`` becomes ``50.00%``. +* ``Decimal(1) / Decimal(2)`` becomes ``50.00%``. +* ``not a number`` returns ``not a number``. + +You can pass in either an integer or a string representation of a number. +**Argument:** Decimal precision can be set utilizing the precision optional argument. +Any number type can be passed, but the function will convert to an integer, +following Python's conversion rules. + +For example:: + + {{ value|percentage:3}} + +* ``1.5`` becomes ``150.000%``. + + +Default precision is set to 2. \ No newline at end of file