How do I return the 1768
value in the currency format BRL 1.768,00
in the Django view?
def moeda(request):
valor = 1768
# formata o valor
return HttpResponse('Valor: %s' % valor)
How do I return the 1768
value in the currency format BRL 1.768,00
in the Django view?
def moeda(request):
valor = 1768
# formata o valor
return HttpResponse('Valor: %s' % valor)
There are two simple ways to do this, as follows:
1) using find:
from django.utils.formats import localize
def moeda(request):
valor = 1768
valor = localize(valor)
return HttpResponse('Valor: %s' % valor)
# resultado: Valor: 1.768,00
2) using locale:
import locale
def moeda(request):
valor = 1768
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
valor = locale.currency(valor, grouping=True, symbol=None)
return HttpResponse('Valor: %s' % valor)
# resultado: Valor: 1.768,00
My strategy is simpler and more direct:
I use the standard formatting (the same as the dollar) and only I '.' and ',':
def real_br_money_mask(my_value):
a = '{:,.2f}'.format(float(my_value))
b = a.replace(',','v')
c = b.replace('.',',')
return c.replace('v','.')
The cool thing about this approach is that you do not have to do any import.