/*
 * Extenso.java
 * Classe Extenso - Calcula o valor por extenso
 * Produced by : Emilio Carlos Lopes de Lemos
 * Exemplo de chamada 
 * System.out.println(Extenso.Texto(123));
 *
*/
package empack;

public final class Extenso {

  public static String Texto(double vlr) {
    double valor = vlr;
    Double db = new Double(vlr);
    String texto = "";
    String centavos = "";
    String num = new String();
    String txt = new String();
    int len;
    int pos;   
    int ponto;
    int x;
    boolean ParteInteira = false;

    if ( vlr == 0)
      return "";
            
    // Convertendo o valor para string
//    txt = txt.valueOf(valor);
    txt = Str(valor);
               
    // separando a parte dos centavos
    ponto = txt.indexOf('.');
    
    if (ponto != -1) {
       centavos = txt.substring(ponto+1); // separando os centavos
       txt = txt.substring(0,ponto);     // e a parte inteira do numero
    }

    txt      = strZero(txt,9); 
    centavos = "0"+padr(centavos,1);

    len = txt.length();
    len = (len / 3);
       
    // Separando os grupos de unidades,
    // dezenas, centenas e milhares

    pos = 0;
    while (pos <= txt.length()-1) {
       num = txt.substring(pos,pos + 3);
       x = Integer.parseInt(num);
       if ( x > 0 ) {
         ParteInteira = true;
         texto = texto + Resultado(num);
       
         if (len == 3) // grupo dos milhoes
         {
           if (x > 0) {
             if ( x == 1 )
               texto = texto + " milhao";
             else
               texto = texto + " milhoes";
             num = txt.substring(pos+3);  // verificar as 6 posicoes 
             x   = Integer.parseInt(num); // restantes
             if ( x >= 0) {
               if ( x == 0 ) 
                 texto = texto + " de";
               else
                 texto = texto + " e";
             }
           }
         }
         else if(len == 2) // grupo dos milhares
         {
             if (x > 0)      
                 texto = texto + " mil";
  
             num = txt.substring(pos+3);  // verificar as centenas
             x   = Integer.parseInt(num); // restantes
             if (x > 0)
               texto = texto + " e";
         }
         else 
         {
         } 
       }       
       pos += 3;
      
       len--;
       
    } // while

    //
    x = (int) valor; // pegando apenas a parte inteira
    if (x >= 1)
    {
      if (x == 1)
        texto = texto + " real";
      else
        texto = texto + " reais";
    }

    // encontrando os centavos
    num =  centavos;
    x = Integer.parseInt(num);
    
    if (x > 0) {

      if (ParteInteira)
        texto = texto + " e";
        
      texto = texto + Resultado(num); 

      if (x == 1) 
        texto = texto + " centavo";
      else 
        texto = texto + " centavos";
    }

    // Retirando espacos em branco em excesso
    texto = texto.substring(1);
    return texto;
    
  }  

   static private String Resultado(String num) {
      String unidade[] = {"um",
    				"dois",
				"tres",
				"quatro",
				"cinco",
				"seis",
				"sete",
				"oito",
				"nove",
				"dez",
                        "onze",
    				"doze",
				"treze",
				"catorze",
				"quinze",
				"desesseis",
				"desessete",
				"dezoito",
				"dezenove"};
				                    
     String dezena [] = {"dez",
    				"vinte",
				"trinta",
				"quarenta",
				"cinquenta",
				"sessenta",
				"setenta",
				"oitenta",
				"noventa"};
			 
    String centena[] = {"cento",
                        "duzentos",
                        "trezentos",
                        "quatrocentos",
                        "quinhentos",
                        "seiscentos",
                        "setecentos",
                        "oitocentos",
                        "novecentos"};

    int cento = Integer.parseInt(num.substring(0,1));
    int dez   = Integer.parseInt(num.substring(1,2));
    int unid  = Integer.parseInt(num.substring(2,3));
    String txt = ""; 
    int n = (int) Integer.parseInt(num);

    // encontrando a centena  
    if (n == 100) {
       txt = txt + " cem"; 
    }
    else { 
       if (cento == 1) {
          txt = txt + " cento";
       }
       else { 
         if (cento != 0) {
            txt = txt + " " + centena[cento-1]; 

         }
       }
    }

    if (( cento > 0) & (n != 100))
      if (( dez > 0) | (unid > 0))
        txt = txt + " e";
      
    // encontrando a dezena
    if (dez == 1) { // numeros de 10 a 19
       txt = txt + " " + unidade[9 + unid];
    }
    else { 
      if (dez == 0) { // encontrar apenas a unidade
        if (unid > 0) 
          txt = txt + " " + unidade[unid - 1];
      }
      else { // encontrar dezena e unidade 
        txt = txt + " " + dezena[dez-1];
        if (unid > 0) {
          txt = txt + " e " + unidade[unid - 1];        
        }
      } // else
    }       
    return txt;
  } // Resultado

  /*
    strZero - Preenche com zeros a esquerda
  */
  static private String strZero(String s, int tam) {
    while (s.length() < tam) {
      s = "0"+s;
    }
    return s;
  }

  static String Str(double valor) {
    String s1 = Double.toString(valor);
    String s2 = s1;
    int pos = s1.indexOf("E");
    int expoente;
    int x,dif;
    if (pos != -1) {

      expoente = Integer.parseInt(s1.substring(pos+1,pos+2));
      
      s2 = "";
      x = s1.indexOf(".");
      s2 = s1.substring(0,x)+s1.substring(x+1,pos);
      s2 = padr(s2,expoente+2);
      s2 = s2.substring(0,expoente+1)+"."+s2.substring(expoente+1);
    }
    return s2;  
  }

  static String padr(String s, int l)  {
    String st = s;
    for (int i = s.length(); i <= l ; i++) 
      st = st + "0";
    return st;  
  }
  
}
