summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/templatetags/templatetags/number_to_word.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/templatetags/templatetags/number_to_word.py b/lib/templatetags/templatetags/number_to_word.py
new file mode 100644
index 0000000..c153932
--- /dev/null
+++ b/lib/templatetags/templatetags/number_to_word.py
@@ -0,0 +1,29 @@
+from django.utils.translation import ungettext, ugettext as _
+import re
+from django import template
+from django.utils.safestring import mark_safe
+register = template.Library()
+
+
+
+SPECIAL_CASES = (_('ten'), _('eleven'), _('twelve'), _('thirteen'), _('fourteen'), _('fifteen'), _('sixteen'), _('seventeen'), _('eighteen'), _('nineteen'),)
+BASE_INT = (_('twenty'), _('thirty'), _('forty'), _('fifty'), _('sixty'), _('seventy'), _('eighty'), _('ninety'))
+PRIME_NUM = (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'),)
+
+@register.filter
+def number_to_word(value):
+ """
+ For numbers 1-99, returns the number spelled out.
+ """
+ value = str(value)
+ if len(value) == 2:
+ if int(value[:1]) == 1:
+ word = SPECIAL_CASES[int(value[1:])]
+ return word
+ else:
+ word = BASE_INT[int(value[:1])-2]
+ word = word + '-'+str(PRIME_NUM[int(value[1:])-1])
+ else:
+ word = PRIME_NUM[int(value[:1])-1]
+ return word
+ \ No newline at end of file