associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

Revisão 4e 2005-05-24 17:25:25

Excluir mensagem

FormatarNumeros

Neste exemplo usei o modulo locale para criar formatações de numeros e moedas no padrão do Brasil

Segue abaixo o codigo:

   1 import locale
   2 
   3 sal=2500.87
   4 aumento=0.25
   5 perc=10
   6 vendas=4800.9
   7 prejuizo=-3200
   8 print "locale", "=>", locale.setlocale(locale.LC_ALL, "")
   9 
  10 print '------Formato numero Float------'
  11 print locale.format("%1.0f",sal,0)
  12 print locale.format("%1.2f",sal,0)
  13 print locale.format("%1.3f",sal,0)
  14 print locale.format("%1.2f",(sal-10000),0)
  15 
  16 print '------Formato numero Porc-------'
  17 print locale.format("%2.2f%%",(aumento * 100),0)
  18 print locale.format("%2.3f%%",(aumento * 100),0)
  19 print locale.format("%1.2f%%",(perc * 100),1)
  20 print locale.format("%1.3f%%",(perc * 100),1)
  21 
  22 print '------Formato moeda ------------'
  23 info=locale.localeconv()
  24 print info['currency_symbol'],locale.format("%1.2f",vendas,1)
  25 print info['currency_symbol'],locale.format("%1.3f",vendas,1)
  26 print info['currency_symbol'],locale.format("%1.2f",prejuizo,1)
  27 print info['currency_symbol'],locale.format("%1.3f",prejuizo,1)
  28 
  29 print 'Simbolo internacional da moeda no pais => ',info["int_curr_symbol"]