Login

Currency filter

Author:
kljensen
Posted:
January 17, 2008
Language:
Python
Version:
.96
Tags:
filter templatetags money currency dollars
Score:
2 (after 4 ratings)

Formats a number in the local currency format. E.g., if foo is equal to 49277, then

{{ foo|currency }}

would print

$49,277

If your locale is the U.S. You can use this filter in your templates as described in the Django documentation

1
2
3
4
5
6
7
8
9
from django import template
import locale
locale.setlocale(locale.LC_ALL, '')
register = template.Library()
 
 
@register.filter()
def currency(value):
    return locale.currency(value, grouping=True)

More like this

Comments

MasonM (on July 24, 2008):
<p>Cool, I didn't know about the locale module. I had to change line 3 to "locale.setlocale(locale.LC_ALL, 'en_US')" for this to work on a shared server I use.</p>

#

cornbread (on August 14, 2008):
<p>This works great on my ubuntu-hardy laptop but I can't get it to work on my ubuntu-gutsy server. </p> <p>I get some kind of a Locale C error.</p>

#

cornbread (on August 18, 2008):
<p>FIGURED IT OUT!!!</p> <p>I had to set up the language pack on my ubuntu-server... Apparently this doesn't get done by default on the server version of ubuntu.</p> <p>sudo ./install-language-pack en_US then restart x and you're golden.</p>

#

gourneau (on September 21, 2008):
<p>Thanks for this filter.</p> <p>I also had to use change line 3 to</p> <p>"locale.setlocale(locale.LC_ALL, 'en_US')"</p>

#

dougal (on March 12, 2009):
<p>hmm this doesn't seem to be working for me. Nothing is displayed on the page?</p>

#

dougal (on March 12, 2009):
<p>Not really sure why but I had to change line 9 to;</p> <pre>return locale.currency(value, symbol=False, grouping=True) </pre> <p>and then I added the GBP symbol (£) myself. Otherwise nothing was ever displayed.</p>

#

astur (on August 14, 2009):
<p>Hm... sometimes 'value' in context may be string. That will be better, imho:</p> <pre>return locale.currency(float(value), grouping=True) </pre>

#

backdoc (on May 15, 2010):
<p>My code above lost its formatting. The line numbers are included. So, maybe you can decipher it.</p>

#

lablinux (on November 26, 2010):
<p>i modify your snip to have the money simbol:</p> <p>from django.utils.translation import ugettext</p> <p>from django.utils.safestring import mark_safe</p> <p>I use first import to translate the simbol money (in my django.po)</p> <p>msgid "currency"</p> <p>msgstr "\€"</p> <p>I use the second import to transform "\&amp\€" to "\€"</p> <p>and return this</p> <p>return mark_safe( '%s %s' %(locale.currency(value, symbol=False, grouping=True), ugettext('currency')) )</p> <p>{"1234.56"|currency} return 1.234,55 €</p>

#

mferreira (on August 21, 2011):
<p>I used the current configured language for the locale. Here's what worked for me:</p> <pre>from django import template from django.utils import translation import locale locale.setlocale(locale.LC_ALL, translation.to_locale(translation.get_language())) register = template.Library() @register.filter_function def currency(value): return locale.currency(value, grouping=True) </pre>

#

Please login first before commenting.