Login

dollarize numbers

Author:
alexdong
Posted:
April 4, 2007
Language:
Python
Version:
.96
Tags:
filter money dollar
Score:
0 (after 0 ratings)

Usage: {{ price|dollarize }}

1
2
3
4
5
6
@register.filter
def dollarize(c):
    if c>0:
        return "$%s"%c
    else:
        return "-$%s"%(-1*c)

More like this

  1. Currency filter by kljensen 7 years, 6 months ago
  2. (en-US) Humanized Decimal Field by ActionScripted 7 years, 1 month ago
  3. sublist by nofactor 7 years, 8 months ago
  4. Significant digits filter by joelegner 6 years, 4 months ago
  5. restrict user access to modeladmin via metaclass by code_shogan 4 years, 5 months ago

Comments

greencoder (on July 17, 2007):

Unless I did something wrong, it didn't return two decimal places for integers and single point precision numbers. This did the trick:

def dollarize(c):
  if c > 0:
    return "$%.2f" % c
  else:
    return "-$%.2f" % (-1*c)

#

Please login first before commenting.