Ticket #17662: django_percentage_take_two

File django_percentage_take_two, 4.0 KB (added by James Reynolds, 12 years ago)

humanize percentage using number_format

Line 
1diff -r 385d1f81c5c7 -r 9fed087a8a42 django/contrib/humanize/templatetags/humanize.py
2--- a/django/contrib/humanize/templatetags/humanize.py Mon Feb 06 02:00:19 2012 +0000
3+++ b/django/contrib/humanize/templatetags/humanize.py Wed Feb 08 16:36:14 2012 -0500
4@@ -222,3 +222,21 @@
5 return ungettext(
6 u'an hour from now', u'%(count)s hours from now', count
7 ) % {'count': count}
8+
9+@register.filter
10+def percentage(value, precision=2):
11+ """
12+ Returns Percentage representation of number types.
13+ Takes an optional integer argument for decimal point accuracy.
14+ Upon failure returns value.
15+ """
16+ try:
17+ precision = int(precision)
18+ value = float(value)*100
19+ except (TypeError, ValueError):
20+ return value
21+ value = round(value, precision)
22+ return number_format(value,decimal_pos=precision)+u'%'
23+ #places = ':.{precision}%'.format(precision=precision)
24+ #perc_holder = '{' + places + '}'
25+ #return perc_holder.format(value)
26diff -r 385d1f81c5c7 -r 9fed087a8a42 django/contrib/humanize/tests.py
27--- a/django/contrib/humanize/tests.py Mon Feb 06 02:00:19 2012 +0000
28+++ b/django/contrib/humanize/tests.py Wed Feb 08 16:36:14 2012 -0500
29@@ -109,6 +109,31 @@
30 someday_result, u"I'm not a date value", None)
31 self.humanize_tester(test_list, result_list, 'naturalday')
32
33+ def test_percentage(self):
34+ from fractions import Fraction
35+ from decimal import Decimal
36+ fl_value = 1.5
37+ int_value = 10
38+ int_fraction = Fraction(1)/2
39+ int_decimal = Decimal(1) / Decimal(2)
40+ notnumber = u"I'm not a number"
41+ long_float = .123456789
42+
43+ test_list = (fl_value, int_value, int_fraction, int_decimal, long_float,notnumber, None)
44+ result_list = (u'150.00%', u'1000.00%', u'50.00%',u'50.00%',u'12.35%',
45+ notnumber, None)
46+ self.humanize_tester(test_list, result_list, 'percentage')
47+ result_list = (u'150.000%', u'1000.000%', u'50.000%',u'50.000%',u'12.346%',
48+ notnumber, None)
49+ self.humanize_tester(test_list, result_list, 'percentage:3')
50+ result_list = (fl_value, int_value, int_fraction,int_decimal,long_float,
51+ notnumber, None)
52+ self.humanize_tester(test_list, result_list, "percentage:'a'")
53+ with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
54+ result_list = (u'150.000%', u'1,000.000%', u'50.000%',u'50.000%',u'12.346%',
55+ notnumber, None)
56+ self.humanize_tester(test_list, result_list, 'percentage:3')
57+
58 def test_naturalday_tz(self):
59 from django.contrib.humanize.templatetags.humanize import naturalday
60
61@@ -200,3 +225,5 @@
62 finally:
63 humanize.datetime = orig_humanize_datetime
64 timesince.datetime = orig_timesince_datetime
65+
66+
67diff -r 385d1f81c5c7 -r 9fed087a8a42 docs/ref/contrib/humanize.txt
68--- a/docs/ref/contrib/humanize.txt Mon Feb 06 02:00:19 2012 +0000
69+++ b/docs/ref/contrib/humanize.txt Wed Feb 08 16:36:14 2012 -0500
70@@ -137,3 +137,32 @@
71 * ``3`` becomes ``3rd``.
72
73 You can pass in either an integer or a string representation of an integer.
74+
75+.. templatefilter:: percentage
76+
77+percentage
78+----------
79+
80+Converts a number to a percentage.
81+Takes an optional integer argument for decimal point accuracy.
82+
83+Examples:
84+
85+* ``1.5`` becomes ``150.00%``.
86+* ``Fraction(1)/2`` becomes ``50.00%``.
87+* ``Decimal(1) / Decimal(2)`` becomes ``50.00%``.
88+* ``not a number`` returns ``not a number``.
89+
90+You can pass in either an integer or a string representation of a number.
91+**Argument:** Decimal precision can be set utilizing the precision optional argument.
92+Any number type can be passed, but the function will convert to an integer,
93+following Python's conversion rules.
94+
95+For example::
96+
97+ {{ value|percentage:3}}
98+
99+* ``1.5`` becomes ``150.000%``.
100+
101+
102+Default precision is set to 2.
103\ No newline at end of file
Back to Top