# Convert numbers in USA notation to brazilian notation

Author:
eOliva
Posted:
October 7, 2008
Language:
Python
Version:
1.0
Score:
-1 (after 1 ratings)

This is the converter, just put it as a filter and call it with a float number.

• So, if you have a template variable {{ my_number }} that is "3096.44".
• It will convert to "3.096,44" using the filter {{ my_number|numBR }}.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30``` ```def numBR(value): """ Recebe um ponto flutuante, converte em string e converte para a notação brasileira. Exemplo no template: Supondo que "meu_numero" tem o valor de 100.0: {{ meu_numero|numBR }} ---> 100,00 Supondo que "meu_numero" tem o valor de 3065.49: {{ meu_numero|numBR }} ---> 3.065,49 """ inteiro,decimal = value.split('.') _LEN_INT_OK = len(inteiro) > 3 #no mínimo duas casas decimais, mesmo se no "value" vier uma if len(decimal) == 1: decimal = "%d0" % int(decimal) lBuff =[] #lista buffer for i,numInt in enumerate(inteiro[::-1]): print i+1, numInt if (i+1) % 3 == 0 and _LEN_INT_OK: numInt = "%s." % numInt lBuff.append(numInt) numeroBr = ''.join(lBuff)[::-1] + ',' + decimal return numeroBr ```

## More like this

1. Stuff by NixonDash 1 month ago
2. Add custom fields to the built-in Group model by jmoppel 3 months ago
3. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 6 months, 2 weeks ago
4. Python Django CRUD Example Tutorial by tuts_station 7 months ago
5. Browser-native date input field by kytta 8 months, 2 weeks ago