Skip to content

Instantly share code, notes, and snippets.

@gsiegman
Created October 12, 2012 16:26
Show Gist options
  • Save gsiegman/3880092 to your computer and use it in GitHub Desktop.
Save gsiegman/3880092 to your computer and use it in GitHub Desktop.
@register.filter()
def currency(value):
symbol = getattr(settings, 'CURRENCY_SYMBOL', '$')
thousand_sep = getattr(settings, 'THOUSAND_SEPARATOR', ',')
decimal_sep = getattr(settings, 'DECIMAL_SEPARATOR', '.')
intstr = str(int(value))
f = lambda x, n, acc=[]: f(x[:-n], n, [(x[-n:])]+acc) if x else acc
intpart = thousand_sep.join(f(intstr, 3))
return "%s%s%s%s" % (symbol, intpart, decimal_sep, ("%0.2f" % value)[-2:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment