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
- currency filter without using locale by andzep 11 months, 3 weeks ago
- Currency formatting filter by rafa 2 years, 2 months ago
- currency filter with minus symbol replacement and css class adding by sspross 1 year ago
- Humanize lists of strings in templates by ChipX86 4 years, 8 months ago
- Template filter for formatting negative numbers by pkshiu 2 years, 9 months ago
Comments
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.
#
This works great on my ubuntu-hardy laptop but I can't get it to work on my ubuntu-gutsy server.
I get some kind of a Locale C error.
#
FIGURED IT OUT!!!
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.
sudo ./install-language-pack en_US then restart x and you're golden.
#
Thanks for this filter.
I also had to use change line 3 to
"locale.setlocale(locale.LC_ALL, 'en_US')"
#
hmm this doesn't seem to be working for me. Nothing is displayed on the page?
#
Not really sure why but I had to change line 9 to;
and then I added the GBP symbol (£) myself. Otherwise nothing was ever displayed.
#
Hm... sometimes 'value' in context may be string. That will be better, imho:
#
Good tip. But, it didn't work on my OSX Leopard box without modification. The main points of difference were that I had to use 'en_US.UTF-8' and I was passing in a decimal type. So, I had to add the Decimal code. You can find a supported locale from the command line with this command "locale -a" (credit to "http://stackoverflow.com/questions/1259971/os-locale-support-for-use-in-python").
1 from django import template 2 from decimal import Decimal 3 import locale 4 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') 5 register = template.Library() 6 7 @register.filter() 8 def currency(value): 9 return locale.currency(Decimal(value), grouping=True)
#
My code above lost its formatting. The line numbers are included. So, maybe you can decipher it.
#
i modify your snip to have the money simbol:
from django.utils.translation import ugettext
from django.utils.safestring import mark_safe
I use first import to translate the simbol money (in my django.po)
msgid "currency"
msgstr "€"
I use the second import to transform "&€" to "€"
and return this
return mark_safe( '%s %s' %(locale.currency(value, symbol=False, grouping=True), ugettext('currency')) )
{"1234.56"|currency} return 1.234,55 €
#
I used the current configured language for the locale. Here's what worked for me:
#