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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.