dollarize numbers

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. captcha, verification code by dingdongquan 1 year, 4 months ago
  2. restrict user access to modeladmin via metaclass by code_shogan 1 year, 3 months ago
  3. Significant digits filter by joelegner 3 years, 2 months ago
  4. Template Tag for Retrieving Settings by joshua 5 years, 2 months ago
  5. Humanize lists of strings in templates by ChipX86 4 years, 11 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)

#

(Forgotten your password?)