valeu Elias, mto obrigado pela aten��o, mas o q eu procurava era algo
como a solu��o do Pablo.
obrigado Pablo
On Thu, 3 Mar 2005 15:00:12 -0300 (ART), Pablo Masrua
<[EMAIL PROTECTED]> wrote:
> import java.math.BigInteger;
> import java.math.BigDecimal;
> import java.util.ArrayList;
> import java.util.Iterator;
> import java.text.DecimalFormat;
>
> public class Extenso {
> private ArrayList nro;
> private BigInteger num;
>
> private String Qualificadores[][] = {
> {"centavo", "centavos"},
> {"", ""},
> {"mil", "mil"},
> {"milh�o", "milh�es"},
> {"bilh�o", "bilh�es"},
> {"trilh�o", "trilh�es"},
> {"quatrilh�o", "quatrilh�es"},
> {"quintilh�o", "quintilh�es"},
> {"sextilh�o", "sextilh�es"},
> {"septilh�o", "septilh�es"}
> };
> private String Numeros[][] = {
> {"zero", "um", "dois", "tr�s", "quatro", "cinco",
> "seis", "sete", "oito", "nove", "dez",
> "onze", "doze", "treze", "quatorze", "quinze",
> "desesseis", "desessete", "dezoito", "desenove"},
> {"vinte", "trinta", "quarenta", "cinquenta",
> "sessenta", "setenta", "oitenta", "noventa"},
> {"cem", "cento", "duzentos", "trezentos",
> "quatrocentos", "quinhentos", "seiscentos",
> "setecentos", "oitocentos", "novecentos"}
> };
>
> /**
> * Construtor
> */
> public Extenso() {
> nro = new ArrayList();
> }
>
> /**
> * Construtor
> *
> [EMAIL PROTECTED] dec valor para colocar por extenso
> */
> public Extenso(BigDecimal dec) {
> this();
> setNumber(dec);
> }
>
> /**
> * Constructor for the Extenso object
> *
> [EMAIL PROTECTED] dec valor para colocar por extenso
> */
> public Extenso(double dec) {
> this();
> setNumber(dec);
> }
>
> /**
> * Sets the Number attribute of the Extenso object
> *
> [EMAIL PROTECTED] dec The new Number value
> */
> public void setNumber(BigDecimal dec) {
> // Converte para inteiro arredondando os centavos
> num = dec
> .setScale(2, BigDecimal.ROUND_HALF_UP)
> .multiply(BigDecimal.valueOf(100))
> .toBigInteger();
>
> // Adiciona valores
> nro.clear();
> if (num.equals(BigInteger.ZERO)) {
> // Centavos
> nro.add(new Integer(0));
> // Valor
> nro.add(new Integer(0));
> }
> else {
> // Adiciona centavos
> addRemainder(100);
>
> // Adiciona grupos de 1000
> while (!num.equals(BigInteger.ZERO)) {
> addRemainder(1000);
> }
> }
> }
>
> public void setNumber(double dec) {
> setNumber(new BigDecimal(dec));
> }
>
> /**
> * Description of the Method
> */
> public void show() {
> Iterator valores = nro.iterator();
>
> while (valores.hasNext()) {
> System.out.println(((Integer)
> valores.next()).intValue());
> }
> System.out.println(toString());
> }
>
> /**
> * Description of the Method
> *
> [EMAIL PROTECTED] Description of the Returned Value
> */
> public String toString() {
> StringBuffer buf = new StringBuffer();
>
> int numero = ((Integer) nro.get(0)).intValue();
> int ct;
>
> for (ct = nro.size() - 1; ct > 0; ct--) {
> // Se ja existe texto e o atual n�o � zero
> if (buf.length() > 0 && ! ehGrupoZero(ct)) {
> buf.append(" e ");
> }
> buf.append(numToString(((Integer)
> nro.get(ct)).intValue(), ct));
> }
> if (buf.length() > 0) {
> if (ehUnicoGrupo())
> buf.append(" de ");
> while (buf.toString().endsWith(" "))
> buf.setLength(buf.length()-1);
> if (ehPrimeiroGrupoUm())
> buf.insert(0, "h");
> if (nro.size() == 2 &&
> ((Integer)nro.get(1)).intValue() == 1) {
> buf.append(" real");
> } else {
> buf.append(" reais");
> }
> if (((Integer) nro.get(0)).intValue() != 0) {
> buf.append(" e ");
> }
> }
> if (((Integer) nro.get(0)).intValue() != 0) {
> buf.append(numToString(((Integer)
> nro.get(0)).intValue(), 0));
> }
> return buf.toString();
> }
>
> private boolean ehPrimeiroGrupoUm() {
> if (((Integer)nro.get(nro.size()-1)).intValue() ==
> 1)
> return true;
> return false;
> }
>
> /**
> * Adds a feature to the Remainder attribute of the
> Extenso object
> *
> [EMAIL PROTECTED] divisor The feature to be added to the
> Remainder attribute
> */
> private void addRemainder(int divisor) {
> // Encontra newNum[0] = num modulo divisor,
> newNum[1] = num dividido divisor
> BigInteger[] newNum =
> num.divideAndRemainder(BigInteger.valueOf(divisor));
>
> // Adiciona modulo
> nro.add(new Integer(newNum[1].intValue()));
>
> // Altera numero
> num = newNum[0];
> }
>
> /**
> * Description of the Method
> *
> [EMAIL PROTECTED] ps Description of Parameter
> [EMAIL PROTECTED] Description of the Returned Value
> */
> private boolean temMaisGrupos(int ps) {
> for (; ps > 0; ps--) {
> if (((Integer) nro.get(ps)).intValue() != 0) {
> return true;
> }
> }
>
> return false;
> }
>
> /**
> * Description of the Method
> *
> [EMAIL PROTECTED] ps Description of Parameter
> [EMAIL PROTECTED] Description of the Returned Value
> */
> private boolean ehUltimoGrupo(int ps) {
> return (ps > 0) && ((Integer)nro.get(ps)).intValue()
> != 0 && !temMaisGrupos(ps - 1);
> }
>
> /**
> * Description of the Method
> *
> [EMAIL PROTECTED] Description of the Returned Value
> */
> private boolean ehUnicoGrupo() {
> if (nro.size() <= 3)
> return false;
> if (!ehGrupoZero(1) && !ehGrupoZero(2))
> return false;
> boolean hasOne = false;
> for(int i=3; i < nro.size(); i++) {
> if (((Integer)nro.get(i)).intValue() != 0) {
> if (hasOne)
> return false;
> hasOne = true;
> }
> }
> return true;
> }
>
> boolean ehGrupoZero(int ps) {
> if (ps <= 0 || ps >= nro.size())
> return true;
> return ((Integer)nro.get(ps)).intValue() == 0;
> }
>
> /**
> * Description of the Method
> *
> [EMAIL PROTECTED] numero Description of Parameter
> [EMAIL PROTECTED] escala Description of Parameter
> [EMAIL PROTECTED] Description of the Returned Value
> */
> private String numToString(int numero, int escala) {
> int unidade = (numero % 10);
> int dezena = (numero % 100); //* nao pode dividir
> por 10 pois verifica de 0..19
> int centena = (numero / 100);
> StringBuffer buf = new StringBuffer();
>
> if (numero != 0) {
> if (centena != 0) {
> if (dezena == 0 && centena == 1) {
> buf.append(Numeros[2][0]);
> }
> else {
> buf.append(Numeros[2][centena]);
> }
> }
>
> if ((buf.length() > 0) && (dezena != 0)) {
> buf.append(" e ");
> }
> if (dezena > 19) {
> dezena /= 10;
> buf.append(Numeros[1][dezena - 2]);
> if (unidade != 0) {
> buf.append(" e ");
> buf.append(Numeros[0][unidade]);
> }
> }
> else if (centena == 0 || dezena != 0) {
> buf.append(Numeros[0][dezena]);
> }
>
> buf.append(" ");
> if (numero == 1) {
> buf.append(Qualificadores[escala][0]);
> }
> else {
> buf.append(Qualificadores[escala][1]);
> }
> }
>
> return buf.toString();
> }
>
> /**
> * Para teste
> *
> [EMAIL PROTECTED] args numero a ser convertido
> */
> public static void main(String[] args) {
> char ch1, ch2, ch3;
> if (args.length == 0) {
> System.out.println("Sintax : ...Extenso <numero>");
> return;
> }
> String sequencia = "abcdsd";
> /*
> for (int i = 0; i < sequencia.length()-2; i++) {
> ch1 = sequencia.charAt(i);
> ch2 = sequencia.charAt(i+1);
> ch3 = sequencia.charAt(i+2);
> if ((ch1+1 == ch2 && ch2+1 == ch3) || (ch1-1 ==
> ch2 && ch2-1 == ch3)){
> System.out.println(ch1+" "+ch2+"
> "+ch3);
> }
> }*/
> if (sequencia.indexOf("asd")!=-1){
> System.out.println(sequencia.indexOf("asd"));
> }
> Extenso teste = new Extenso(new
> BigDecimal(args[0]));
> System.out.println("Numero : " + (new
> DecimalFormat().format(Double.valueOf(args[0]))));
> System.out.println("Extenso : " +
> teste.toString().toUpperCase());
> }
> }
>
> --- Elias Sales <[EMAIL PROTECTED]> wrote:
> > de 0 a 99...
> >
> >
> > public class Exercicio16 {
> > public static final void main(String args[]) {
> > int valor=0, digitos=args[0].length();
> > try {
> > valor = Integer.parseInt(args[0]);
> > if (valor<0 || valor>99) {
> > System.out.println("valores validos: 0 ..
> > 99");
> > return;
> > }
> > } catch (NumberFormatException e) {
> > System.out.println("argumento invalido");
> > }
> > System.out.print("> ");
> > if(digitos==2){
> > switch(args[0].charAt(0)) {
> > case '1':
> > switch(valor) {
> > case 10: System.out.println("Dez");
> > return;
> > case 11: System.out.println("Onze");
> > return;
> > case 12: System.out.println("Doze");
> > return;
> > case 13: System.out.println("Treze");
> > return;
> > case 14: System.out.println("Quatorze");
> > return;
> > case 15: System.out.println("Quinze");
> > return;
> > case 16:
> > System.out.println("Dezesseis"); return;
> > case 17: System.out.println("Dezesete");
> > return;
> > case 18: System.out.println("Dezoito");
> > return;
> > case 19: System.out.println("Dezenove");
> > return;
> > }
> > case '2': System.out.print("Vinte"); break;
> > case '3': System.out.print("Trinta"); break;
> > case '4': System.out.print("Quarenta");
> > break;
> > case '5': System.out.print("Cinquenta");
> > break;
> > case '6': System.out.print("Sessenta");
> > break;
> > case '7': System.out.print("Setenta");
> > break;
> > case '8': System.out.print("Oitenta");
> > break;
> > case '9': System.out.print("Noventa");
> > break;
> > }
> > if (valor>10)
> > System.out.print(" e ");
> > }
> > switch(args[0].charAt(digitos-1)) {
> > case '0': if (valor==0)
> > System.out.println("Zero"); break;
> > case '1': System.out.println("Um"); break;
> > case '2': System.out.println("Dois"); break;
> > case '3': System.out.println("Tres"); break;
> > case '4': System.out.println("Quatro"); break;
> > case '5': System.out.println("Cinco"); break;
> > case '6': System.out.println("Seis"); break;
> > case '7': System.out.println("Sete"); break;
> > case '8': System.out.println("Oito"); break;
> > case '9': System.out.println("Nove"); break;
> > }
> > }
> > }
> > ----- Original Message -----
> > From: "Arnaldo Escossio" <[EMAIL PROTECTED]>
> > To: <[email protected]>
> > Sent: Wednesday, March 02, 2005 6:44 PM
> > Subject: [cejug-discussao] valor por extenso
> >
> >
> > Ol� caros amigos,
> >
> > ser� que algu�m tem uma fun��o que retorne um valor
> > por extenso feito em
> > java?
> >
> > encontrei alguns na net mas todos eles eu teria de
> > mexer no c�digo pra
> > ficar bom, e n�o disponho de tempo.
> >
> > Desde j�, meus agradecimentos
> > --
> > Arnaldo Esc�ssio
> > [EMAIL PROTECTED]
> > [EMAIL PROTECTED]
> >
> >
> ----------------------------------------------------------------------------
> > ---------------
> > Ceara' Java User Group
> >
> > Para cancelar sua assinatura, envie um e-mail
> > para:
> > [EMAIL PROTECTED]
> > Para mais informacoes, mande um e-mail para:
> > [EMAIL PROTECTED]
> > Falar com o administrador? e-mail para:
> > [EMAIL PROTECTED]
> >
> >
> >
> >
> -------------------------------------------------------------------------------------------
> > Ceara' Java User Group
> >
> > Para cancelar sua assinatura, envie um e-mail
> > para: [EMAIL PROTECTED]
> > Para mais informacoes, mande um e-mail para:
> > [EMAIL PROTECTED]
> > Falar com o administrador? e-mail para:
> > [EMAIL PROTECTED]
> >
> >
> >
>
> __________________________________________________
> Converse com seus amigos em tempo real com o Yahoo! Messenger
> http://br.download.yahoo.com/messenger/
>
> -------------------------------------------------------------------------------------------
>
> Ceara' Java User Group
>
> Para cancelar sua assinatura, envie um e-mail para: [EMAIL PROTECTED]
>
> Para mais informacoes, mande um e-mail para: [EMAIL PROTECTED]
>
> Falar com o administrador? e-mail para: [EMAIL PROTECTED]
>
>
--
Arnaldo Esc�ssio
[EMAIL PROTECTED]
[EMAIL PROTECTED]
-------------------------------------------------------------------------------------------
Ceara' Java User Group
Para cancelar sua assinatura, envie um e-mail para: [EMAIL PROTECTED]
Para mais informacoes, mande um e-mail para: [EMAIL PROTECTED]
Falar com o administrador? e-mail para: [EMAIL PROTECTED]