Convert numbers in USA notation to brazilian notation

 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. RPN template math by durka 4 years, 7 months ago
  2. Tags & filters for rendering search results by exogen 5 years, 2 months ago
  3. Sorl Thumbnail + Amazon S3 by skoczen 4 years ago
  4. Number generator to autofill a field by pizte 2 years, 11 months ago
  5. TemplateTag to Split a List into Uniform Chunks by cmcavoy 5 years, 2 months ago

Comments

(Forgotten your password?)