Gostaria uma ajuda num programa que leia qualquer valores por extenso
Taí(é só adaptar a sua necessidade):
//package brazilutils.currency.br
import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.*;
import java.text.DecimalFormat;
/**
* Titulo: NumeroPorExtenso <p>
* Note:This class was writen in portuguese
* Descrição: Programa converte um numero para o valor em extenso <p>
*
*
*@author Ironlynx based on code found on PortalJava(www.portaljava.com)
* owned by Sérgio Eduardo Rodrigues
*@version 0.1
*@created 20/04/2005
*/
public class NumeroPorExtenso {
private List numeroLista;
private BigInteger num;
private String Qualificadores[][] = {//array de 2 linhas e 14 colunas[2][14]
{"milésimo de real","milésimos de real"},//[0][0] e [0][1]
{"centavo", "centavos"},//[1][0] e [1][1]
{"", ""},//[2][0],[2][1]
{"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"},
{"setilhão", "setilhões"},
{"octilhão","octilhões"},
{"nonilhão","nonilhões"},
{"decilhão","decilhões"}
};
private String Numeros[][] = {
{"zero", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove", "dez",
"onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"},
{"vinte", "trinta", "quarenta", "cinqüenta", "sessenta", "setenta", "oitenta", "noventa"},
{"cem", "cento", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos",
"setecentos", "oitocentos", "novecentos"}
};
/**
* Construtor
*/
public NumeroPorExtenso() {
numeroLista = new ArrayList();
}
/**
* Construtor
*
*@param dec valor para colocar por extenso
*/
public NumeroPorExtenso(BigDecimal dec) {
this();
setNumero(dec);
}
/**
* Constructor for the Extenso object
*
*@param dec valor para colocar por extenso
*/
public NumeroPorExtenso(double dec) {
this();
setNumero(dec);
}
public void setNumero(BigDecimal dec) {
// Converte para inteiro arredondando os centavos
num = dec
.setScale(2,BigDecimal.ROUND_HALF_UP)// até 3 casas depois da vírgula
.multiply(BigDecimal.valueOf(100))
.toBigInteger();
// Adiciona valores
numeroLista.clear();
if (num.equals(BigInteger.ZERO)) {
// Centavos
numeroLista.add(new Integer(0));
// Valor
numeroLista.add(new Integer(0));
}
else {
// Adiciona centavos
addRemainder(100);
// Adiciona grupos de 1000
while (!num.equals(BigInteger.ZERO)) {
addRemainder(1000);
}
}
}
public void setNumero(double dec) {
setNumero(new BigDecimal(dec));
}
/**
* Description of the Method
*
*@return Description of the Returned Value
*/
public String toString() {
StringBuilder buf = new StringBuilder();
int numero = ((Integer) numeroLista.get(0)).intValue();
int count;
for (count = numeroLista.size() - 1; count > 0; count--) {
// Se ja existe texto e o atual não é zero
if (buf.length() > 0 && ! ehGrupoZero(count)) {
buf.append(" e ");
}
buf.append(numToString(((Integer) numeroLista.get(count)).intValue(), count));
}
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 (numeroLista.size() == 2 && ((Integer)numeroLista.get(1)).intValue() == 1) {
buf.append(" real");
} else {
buf.append(" reais");
}
if (((Integer) numeroLista.get(0)).intValue() != 0) {
buf.append(" e ");
}
}
if (((Integer) numeroLista.get(0)).intValue() != 0) {
buf.append(numToString(((Integer) numeroLista.get(0)).intValue(), 0));
}
return buf.toString();
}
private boolean ehPrimeiroGrupoUm() { //milhar
if (((Integer)numeroLista.get(numeroLista.size()-1)).intValue() == 1)
return true;
return false;
}
/**
* Adds a feature to the Remainder attribute of the Extenso object
*
*@param 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
numeroLista.add(new Integer(newNum[1].intValue()));
// Altera numero
num = newNum[0];
}
/**
* Description of the Method
*
*@param ps Description of Parameter
*@return Description of the Returned Value
*/
private boolean temMaisGrupos(int ps) {
for (; ps > 0; ps--) {
if (((Integer) numeroLista.get(ps)).intValue() != 0) {
return true;
}
}
return false;
}
/**
* Description of the Method
*
*@param ps Description of Parameter
*@return Description of the Returned Value
*/
private boolean ehUltimoGrupo(int ps) {
return (ps > 0) && ((Integer)numeroLista.get(ps)).intValue() != 0 && !temMaisGrupos(ps - 1);
}
/**
* Description of the Method
*
*@return Description of the Returned Value
*/
private boolean ehUnicoGrupo() {
if (numeroLista.size() <= 3)
return false;
if (!ehGrupoZero(1) && !ehGrupoZero(2))
return false;
boolean hasOne = false;
for(int i=3; i < numeroLista.size(); i++) {
if (((Integer)numeroLista.get(i)).intValue() != 0) {
if (hasOne)
return false;
hasOne = true;
}
}
return true;
}
boolean ehGrupoZero(int ps) {
if (ps <= 0 || ps >= numeroLista.size())
return true;
return ((Integer)numeroLista.get(ps)).intValue() == 0;
}
/**
* Description of the Method
*
*@param numero Description of Parameter
*@param escala Description of Parameter
*@return 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);
//int milhar= (numero%1000);
StringBuilder buf = new StringBuilder();
if (numero != 0) {//se zero não for digitado!!!
if (centena != 0) {
if (dezena == 0 && centena == 1) {
buf.append(Numeros[2][0]);//cem!
}
else {
buf.append(Numeros[2][centena]); //todos as demais centenas
}
}
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) { //até 1
buf.append(Qualificadores[escala][0]);//singular
}
else { //maior do que 1 adiciono o plural
buf.append(Qualificadores[escala][1]);//plural
}
}
return buf.toString();
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Digitar no console : java NumeroPorExtenso <numero>");
return;
}
NumeroPorExtenso teste = new NumeroPorExtenso(new BigDecimal(args[0]));
System.out.println("Numero : " + (new DecimalFormat().format(Double.valueOf(args[0]))));
System.out.println("Extenso : " + teste.toString());
}
}//fim da classe NumeroPorExtenso
Mas não crie vários tópicos com a mesma perguunta!!! :evil: