from django import template
register = template.Library()
@register.filter()
def currency(value, grouping=True):
"""
e.g.
import locale
locale.setlocale(locale.LC_ALL, 'de_CH.UTF-8')
currency(123456.789) # Fr. 123'456.79
currency(-123456.789) # Fr. -123'456.79
"""
result = locale.currency(value, grouping=grouping)
# add css class if value is negative
if value < 0:
# replace the minus symbol if needed
if result[-1] == '-':
length = len(locale.nl_langinfo(locale.CRNCYSTR))
result = '%s-%s' % (result[0:length], result[length:-1])
return '%s' % result
return result