Skip to content

Commit

Permalink
Fixed #1684 -- Added apnumber template filter in django.contrib.human…
Browse files Browse the repository at this point in the history
…ize. Thanks, ubernostrum

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3077 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Jun 4, 2006
1 parent fb537e1 commit b8fd9a4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
14 changes: 14 additions & 0 deletions django/contrib/humanize/templatetags/humanize.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,3 +48,17 @@ def intword(value):
return '%.1f trillion' % (value / 1000000000000.0) return '%.1f trillion' % (value / 1000000000000.0)
return value return value
register.filter(intword) register.filter(intword)

def apnumber(value):
"""
For numbers 1-9, returns the number spelled out. Otherwise, returns the
number. This follows Associated Press style.
"""
try:
value = int(value)
except ValueError:
return value
if not 0 < value < 10:
return value
return ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')[value-1]
register.filter(apnumber)
26 changes: 20 additions & 6 deletions docs/add_ons.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ To activate these filters, add ``'django.contrib.english'`` to your
``INSTALLED_APPS`` setting. Once you've done that, use ``{% load english %}`` ``INSTALLED_APPS`` setting. Once you've done that, use ``{% load english %}``
in a template, and you'll have access to these filters: in a template, and you'll have access to these filters:


ordinal apnumber
------- --------


Converts an integer to its ordinal as a string. For numbers 1-9, returns the number spelled out. Otherwise, returns the
number. This follows Associated Press style.


Examples: Examples:


* ``1`` becomes ``'1st'``. * ``1`` becomes ``'one'``.
* ``2`` becomes ``'2nd'``. * ``2`` becomes ``'two'``.
* ``3`` becomes ``'3rd'``. * ``10`` becomes ``10``.


You can pass in either an integer or a string representation of an integer. You can pass in either an integer or a string representation of an integer.


Expand Down Expand Up @@ -96,6 +97,19 @@ Values up to 1000000000000000 (one quadrillion) are supported.


You can pass in either an integer or a string representation of an integer. You can pass in either an integer or a string representation of an integer.


ordinal
-------

Converts an integer to its ordinal as a string.

Examples:

* ``1`` becomes ``'1st'``.
* ``2`` becomes ``'2nd'``.
* ``3`` becomes ``'3rd'``.

You can pass in either an integer or a string representation of an integer.

flatpages flatpages
========= =========


Expand Down

0 comments on commit b8fd9a4

Please sign in to comment.