Created
March 14, 2012 20:49
-
-
Save pikhovkin/2039405 to your computer and use it in GitHub Desktop.
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment