MLMPBR commited on
Commit
e1c9b0a
·
verified ·
1 Parent(s): 7aca39d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -21,13 +21,28 @@ def converter_para_float(valor):
21
  if isinstance(valor, (int, float)): return float(valor)
22
  try:
23
  v = str(valor).replace('R$', '').strip()
24
- # Se for formato brasileiro (ex: 1.000,50), removemos o ponto e trocamos vírgula por ponto
 
25
  if ',' in v and '.' in v and v.rfind(',') > v.rfind('.'):
26
  v = v.replace('.', '').replace(',', '.')
27
- elif ',' in v and '.' not in v: # Apenas vírgula (ex: 1000,50)
28
- v = v.replace(',', '.')
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  return float(v)
30
- except: return 0.0
 
31
 
32
  def limpar_colunas(df):
33
  if df.empty: return df
 
21
  if isinstance(valor, (int, float)): return float(valor)
22
  try:
23
  v = str(valor).replace('R$', '').strip()
24
+
25
+ # Formato brasileiro (ex: 1.234.567,89 ou 1.000,00)
26
  if ',' in v and '.' in v and v.rfind(',') > v.rfind('.'):
27
  v = v.replace('.', '').replace(',', '.')
28
+ # Formato americano (ex: 1,234,567.89 ou 1,000.00)
29
+ elif ',' in v and '.' in v and v.rfind('.') > v.rfind(','):
30
+ v = v.replace(',', '')
31
+ # Apenas vírgula (ex: 1000,50 -> BR) ou (1,000,000 -> US)
32
+ elif ',' in v and '.' not in v:
33
+ if v.count(',') > 1: # Múltiplas vírgulas = padrão americano
34
+ v = v.replace(',', '')
35
+ else: # Uma vírgula = decimal brasileiro
36
+ v = v.replace(',', '.')
37
+ # Apenas ponto (ex: 1000.50 -> US) ou (1.000.000 -> BR)
38
+ elif '.' in v and ',' not in v:
39
+ if v.count('.') > 1: # Múltiplos pontos = milhares brasileiros
40
+ v = v.replace('.', '')
41
+ # Se for só um ponto (ex: 539.72), o Python lê nativamente sem alterar.
42
+
43
  return float(v)
44
+ except:
45
+ return 0.0
46
 
47
  def limpar_colunas(df):
48
  if df.empty: return df