- Author:
- eOliva
- Posted:
- October 7, 2008
- Language:
- Python
- Version:
- 1.0
- Tags:
- convert float usa br brazil
- 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
|
Comments
Please login first before commenting.