Ticket #17662: django_percentage_take_two.diff
File django_percentage_take_two.diff, 4.0 KB (added by , 13 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
diff -r 385d1f81c5c7 -r 9fed087a8a42 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)*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 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 = 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 112 137 def test_naturalday_tz(self): 113 138 from django.contrib.humanize.templatetags.humanize import naturalday 114 139 … … 200 225 finally: 201 226 humanize.datetime = orig_humanize_datetime 202 227 timesince.datetime = orig_timesince_datetime 228 229 -
docs/ref/contrib/humanize.txt
diff -r 385d1f81c5c7 -r 9fed087a8a42 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