- Author:
- pikhovkin
- Posted:
- March 14, 2012
- Language:
- Python
- Version:
- Not specified
- Score:
- 2 (after 2 ratings)
Converts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | # number_human.py
from django.template import Library, Node
register = Library()
@register.simple_tag
def number_human(value, separator=',', precision=2, delimeter_count=3, decimal_separator='.'):
""" Converts an integer or floating-point number or a string to a string
containing the delimiter character (default comma)
after every delimeter_count digits (by default 3 digits).
For example:
{% load number_human %}
String: {% number_human '1234567890.5' ' ' %}
Float: {% number_human 1234567890.5 ' ' %}
Integer {% number_human 1234567890 ' ' %}
String: {% number_human '1234567890,5' ' ' 1 3 ',' %}
"""
f = ''
if isinstance(value, float):
negative = value < 0
s = '%s.%df' % ('%', precision) % (abs(value))
p = s.find(decimal_separator)
if p > -1:
f = s[p:]
s = s[:p]
elif isinstance(value, int):
negative = value < 0
s = str(abs(value))
else:
negative = False
s = value
p = s.find(decimal_separator)
if p > -1:
f = s[p:p + precision + 1]
if f == decimal_separator:
f = ''
s = s[:p]
groups = []
while s:
groups.insert(0, s[-delimeter_count:])
s = s[:-delimeter_count]
return '%s%s%s' % ('-' if negative else '', separator.join(groups), f)
# tests.py
from django.test import TestCase
from django import template
class NumberHumanTest(TestCase):
def setUp(self):
self.context = template.Context()
def test_number_human(self):
template_content = "{% load number_human %}[{% number_human '1234567890.5' ' ' %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567 890.5]')
template_content = "{% load number_human %}[{% number_human 1234567890.5 ' ' 1 %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567 890.5]')
template_content = "{% load number_human %}[{% number_human 123456789.5 ' ' 1 %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[123 456 789.5]')
template_content = "{% load number_human %}[{% number_human 12345678.5 ' ' 1 %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[12 345 678.5]')
template_content = "{% load number_human %}[{% number_human 1234567.5 ' ' 1 %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567.5]')
template_content = "{% load number_human %}[{% number_human 1234567890 ' ' %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567 890]')
template_content = "{% load number_human %}[{% number_human 123456789 ' ' %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[123 456 789]')
template_content = "{% load number_human %}[{% number_human 12345678 ' ' %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[12 345 678]')
template_content = "{% load number_human %}[{% number_human 1234567 ' ' %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567]')
template_content = "{% load number_human %}[{% number_human 1234567890.1234567890 ' ' 6 %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567 890.123457]')
template_content = "{% load number_human %}[{% number_human '1234567890.1234567890' ' ' 10 %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567 890.1234567890]')
template_content = "{% load number_human %}[{% number_human '1234567890,1234567890' ' ' 10 3 ',' %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567 890,1234567890]')
template_content = "{% load number_human %}[{% number_human '1234567890,1234567890' ' ' 10 3 ',' %}]"
value = template.Template(template_content).render(self.context)
self.assertEqual(value, '[1 234 567 890,1234567890]')
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.