Ticket #4976: humanize.diff
File humanize.diff, 3.4 KB (added by , 17 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
12 12 """ 13 13 try: 14 14 value = int(value) 15 except ValueError:15 except (ValueError, TypeError): 16 16 return value 17 17 t = (_('th'), _('st'), _('nd'), _('rd'), _('th'), _('th'), _('th'), _('th'), _('th'), _('th')) 18 18 if value % 100 in (11, 12, 13): # special case … … 39 39 numbers over 1 million. For example, 1000000 becomes '1.0 million', 1200000 40 40 becomes '1.2 million' and '1200000000' becomes '1.2 billion'. 41 41 """ 42 value = int(value) 42 try: 43 value = int(value) 44 except (ValueError, TypeError): 45 return value 46 43 47 if value < 1000000: 44 48 return value 45 49 if value < 1000000000: … … 61 65 """ 62 66 try: 63 67 value = int(value) 64 except ValueError:68 except (ValueError, TypeError): 65 69 return value 66 70 if not 0 < value < 10: 67 71 return value -
tests/regressiontests/humanize/tests.py
19 19 def test_ordinal(self): 20 20 test_list = ('1','2','3','4','11','12', 21 21 '13','101','102','103','111', 22 'something else' )22 'something else', None) 23 23 result_list = ('1st', '2nd', '3rd', '4th', '11th', 24 24 '12th', '13th', '101st', '102nd', '103rd', 25 '111th', 'something else' )25 '111th', 'something else', 'None') 26 26 27 27 self.humanize_tester(test_list, result_list, 'ordinal') 28 28 29 29 def test_intcomma(self): 30 30 test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25, 31 '100', '1000', '10123', '10311', '1000000', '1234567.1234567' )31 '100', '1000', '10123', '10311', '1000000', '1234567.1234567', None) 32 32 result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25', 33 '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567' )33 '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', 'None') 34 34 35 35 self.humanize_tester(test_list, result_list, 'intcomma') 36 36 37 37 def test_intword(self): 38 38 test_list = ('100', '1000000', '1200000', '1290000', 39 '1000000000','2000000000','6000000000000' )39 '1000000000','2000000000','6000000000000', None) 40 40 result_list = ('100', '1.0 million', '1.2 million', '1.3 million', 41 '1.0 billion', '2.0 billion', '6.0 trillion' )41 '1.0 billion', '2.0 billion', '6.0 trillion', 'None') 42 42 43 43 self.humanize_tester(test_list, result_list, 'intword') 44 44 45 45 def test_apnumber(self): 46 46 test_list = [str(x) for x in range(1, 11)] 47 test_list.append(None) 47 48 result_list = (u'one', u'two', u'three', u'four', u'five', u'six', 48 u'seven', u'eight', u'nine', u'10' )49 u'seven', u'eight', u'nine', u'10', 'None') 49 50 50 51 self.humanize_tester(test_list, result_list, 'apnumber') 51 52