currency filter with minus symbol replacement and css class adding

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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) # <span class="negative">Fr. -123'456.79</span>
    """
    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 '<span class="negative">%s</span>' % result
        
    return result

More like this

  1. Template filter for formatting negative numbers by pkshiu 5 years ago
  2. Currency filter by kljensen 6 years, 3 months ago
  3. Currency Object by Rupe 4 years, 11 months ago
  4. currency filter without using locale by andzep 3 years, 2 months ago
  5. Add CSS class template filter by lazerscience 3 years, 5 months ago

Comments

(Forgotten your password?)