Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #1684 -- Added apnumber template filter in django.contrib.human…

…ize. Thanks, ubernostrum

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3077 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b8fd9a44e5a00ec1a3e857cd20442ec2572335d0 1 parent fb537e1
Adrian Holovaty authored June 04, 2006
14  django/contrib/humanize/templatetags/humanize.py
@@ -48,3 +48,17 @@ def intword(value):
48 48
         return '%.1f trillion' % (value / 1000000000000.0)
49 49
     return value
50 50
 register.filter(intword)
  51
+
  52
+def apnumber(value):
  53
+    """
  54
+    For numbers 1-9, returns the number spelled out. Otherwise, returns the
  55
+    number. This follows Associated Press style.
  56
+    """
  57
+    try:
  58
+        value = int(value)
  59
+    except ValueError:
  60
+        return value
  61
+    if not 0 < value < 10:
  62
+        return value
  63
+    return ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')[value-1]
  64
+register.filter(apnumber)
26  docs/add_ons.txt
@@ -53,16 +53,17 @@ To activate these filters, add ``'django.contrib.english'`` to your
53 53
 ``INSTALLED_APPS`` setting. Once you've done that, use ``{% load english %}``
54 54
 in a template, and you'll have access to these filters:
55 55
 
56  
-ordinal
57  
--------
  56
+apnumber
  57
+--------
58 58
 
59  
-Converts an integer to its ordinal as a string.
  59
+For numbers 1-9, returns the number spelled out. Otherwise, returns the
  60
+number. This follows Associated Press style.
60 61
 
61 62
 Examples:
62 63
 
63  
-    * ``1`` becomes ``'1st'``.
64  
-    * ``2`` becomes ``'2nd'``.
65  
-    * ``3`` becomes ``'3rd'``.
  64
+    * ``1`` becomes ``'one'``.
  65
+    * ``2`` becomes ``'two'``.
  66
+    * ``10`` becomes ``10``.
66 67
 
67 68
 You can pass in either an integer or a string representation of an integer.
68 69
 
@@ -96,6 +97,19 @@ Values up to 1000000000000000 (one quadrillion) are supported.
96 97
 
97 98
 You can pass in either an integer or a string representation of an integer.
98 99
 
  100
+ordinal
  101
+-------
  102
+
  103
+Converts an integer to its ordinal as a string.
  104
+
  105
+Examples:
  106
+
  107
+    * ``1`` becomes ``'1st'``.
  108
+    * ``2`` becomes ``'2nd'``.
  109
+    * ``3`` becomes ``'3rd'``.
  110
+
  111
+You can pass in either an integer or a string representation of an integer.
  112
+
99 113
 flatpages
100 114
 =========
101 115
 

0 notes on commit b8fd9a4

Please sign in to comment.
Something went wrong with that request. Please try again.