summaryrefslogtreecommitdiff
path: root/django/contrib/humanize
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2006-06-04 00:58:39 +0000
committerAdrian Holovaty <adrian@holovaty.com>2006-06-04 00:58:39 +0000
commitfb537e177d7d304d6642ee6005258a82584a8032 (patch)
tree3c5a1d65f3a8b6918b8e5597067a1cd6a56361e7 /django/contrib/humanize
parenta5b7c298164e3c1547988c734d6155c17f57b1d7 (diff)
downloaddjango-fb537e177d7d304d6642ee6005258a82584a8032.tar.gz
Added django.contrib.humanize, a set of template tags for adding a 'human touch' to data. They're documented in add_ons.txt.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3076 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/contrib/humanize')
-rw-r--r--django/contrib/humanize/__init__.py0
-rw-r--r--django/contrib/humanize/templatetags/__init__.py0
-rw-r--r--django/contrib/humanize/templatetags/humanize.py50
3 files changed, 50 insertions, 0 deletions
diff --git a/django/contrib/humanize/__init__.py b/django/contrib/humanize/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/django/contrib/humanize/__init__.py
diff --git a/django/contrib/humanize/templatetags/__init__.py b/django/contrib/humanize/templatetags/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/django/contrib/humanize/templatetags/__init__.py
diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py
new file mode 100644
index 0000000000..d3170f3116
--- /dev/null
+++ b/django/contrib/humanize/templatetags/humanize.py
@@ -0,0 +1,50 @@
+from django import template
+import re
+
+register = template.Library()
+
+def ordinal(value):
+ """
+ Converts an integer to its ordinal as a string. 1 is '1st', 2 is '2nd',
+ 3 is '3rd', etc. Works for any integer.
+ """
+ try:
+ value = int(value)
+ except ValueError:
+ return value
+ t = ('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th')
+ if value % 100 in (11, 12, 13): # special case
+ return '%dth' % value
+ return '%d%s' % (value, t[value % 10])
+register.filter(ordinal)
+
+def intcomma(value):
+ """
+ Converts an integer to a string containing commas every three digits.
+ For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
+ """
+ orig = str(value)
+ new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', str(value))
+ if orig == new:
+ return new
+ else:
+ return intcomma(new)
+register.filter(intcomma)
+
+def intword(value):
+ """
+ Converts a large integer to a friendly text representation. Works best for
+ numbers over 1 million. For example, 1000000 becomes '1.0 million', 1200000
+ becomes '1.2 million' and '1200000000' becomes '1.2 billion'.
+ """
+ value = int(value)
+ if value < 1000000:
+ return value
+ if value < 1000000000:
+ return '%.1f million' % (value / 1000000.0)
+ if value < 1000000000000:
+ return '%.1f billion' % (value / 1000000000.0)
+ if value < 1000000000000000:
+ return '%.1f trillion' % (value / 1000000000000.0)
+ return value
+register.filter(intword)