How do I return a value in the Brazilian currency format in the Django view?

6

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)
    
asked by Paulo 27.05.2015 в 15:32
source

3 answers

6

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
    
answered by 27.05.2015 / 15:36
source
3

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.

    
answered by 13.12.2017 в 00:18
0

Using Python 3 on Windows 7 (already in pt-BR), I found the following result:

Usinglocale.setlocale(locale.LC_ALL,'pt_BR.UTF-8'),resulted:

However, I did not test with Python 2.

    
answered by 25.11.2016 в 18:31