Ticket #17662: django_percentage.diff
File django_percentage.diff, 3.5 KB (added by , 13 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
diff -r 385d1f81c5c7 -r 975534756783 django/contrib/humanize/templatetags/humanize.py
a b 222 222 return ungettext( 223 223 u'an hour from now', u'%(count)s hours from now', count 224 224 ) % {'count': count} 225 226 @register.filter 227 def 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 109 109 someday_result, u"I'm not a date value", None) 110 110 self.humanize_tester(test_list, result_list, 'naturalday') 111 111 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 112 132 def test_naturalday_tz(self): 113 133 from django.contrib.humanize.templatetags.humanize import naturalday 114 134 … … 200 220 finally: 201 221 humanize.datetime = orig_humanize_datetime 202 222 timesince.datetime = orig_timesince_datetime 223 224 -
docs/ref/contrib/humanize.txt
diff -r 385d1f81c5c7 -r 975534756783 docs/ref/contrib/humanize.txt
a b 137 137 * ``3`` becomes ``3rd``. 138 138 139 139 You can pass in either an integer or a string representation of an integer. 140 141 .. templatefilter:: percentage 142 143 percentage 144 ---------- 145 146 Converts a number to a percentage. 147 Takes an optional integer argument for decimal point accuracy. 148 149 Examples: 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 156 You 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. 158 Any number type can be passed, but the function will convert to an integer, 159 following Python's conversion rules. 160 161 For example:: 162 163 {{ value|percentage:3}} 164 165 * ``1.5`` becomes ``150.000%``. 166 167 168 Default precision is set to 2. 169 No newline at end of file