def int_format(value, decimal_points=3, seperator=u'.'):
value = str(value)
if len(value) <= decimal_points:
return value
# say here we have value = '12345' and the default params above
parts = []
while value:
parts.append(value[-decimal_points:])
value = value[:-decimal_points]
# now we should have parts = ['345', '12']
parts.reverse()
# and the return value should be u'12.345'
return seperator.join(parts)
@register.filter('intspace')
def intspace(value):
"""
Converts an integer to a string containing spaces every three digits.
For example, 3000 becomes '3 000' and 45000 becomes '45 000'.
See django.contrib.humanize app
"""
orig = force_unicode(value)
new = re.sub("^(-?\d+)(\d{3})", '\g<1> \g<2>', orig)
if orig == new:
return new
else:
return intspace(new)