Login

Convert numbers in USA notation to brazilian notation

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

More like this

  1. Number generator to autofill a field by pizte 4 years, 8 months ago
  2. RPN template math by durka 6 years, 4 months ago
  3. Name Capitalize Filter by hotani 7 years, 10 months ago
  4. Converts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits) by pikhovkin 2 years, 11 months ago
  5. Template filter that divides a list into exact columns by davmuz 3 years, 1 month ago

Comments

Please login first before commenting.