Por favor,que alguien me ayude
Buenas días.Soy un novato en esto de JAVA y necesito que alguien me ayude. Tengo que crear una clase que te pide una cantidad en euros (con dos decimales) hasta un millón y te lo escriba en letras.Llevo ya bastante tiempo intentándolo (del orden de dos semanas o así) y no sé que hacer,estoy desesperado.
Ejemplo:
Si le meto 3456.24 € me tiene que devolver "tres mil cuatrocientos cincuenta y seis euros con veinticuatro céntimos" (No hay que hacer ninguna conversión a euros,solo escribirle la cantidad).
Estaré enormemente agradecido a quien pueda ayudarme.
Ejemplo:
Si le meto 3456.24 € me tiene que devolver "tres mil cuatrocientos cincuenta y seis euros con veinticuatro céntimos" (No hay que hacer ninguna conversión a euros,solo escribirle la cantidad).
Estaré enormemente agradecido a quien pueda ayudarme.
hola, soy el coordinador de una campaña en contra del matrimonio,, ¿estas segura de conocer bien a tu pareja?, ¿como puedes estar 100% segura de que tu pareja nunca te traicionara?, ¿te vas a ariesgar a tener hijos sin padre?, ademas el divorcio no podra compensar el posible daño que te pudiera hacer tu pareja.
Por favor envia tu respuesta a [email protected]
Por favor envia tu respuesta a [email protected]
Te he dejado un mensaje en tu dirección de correo electrónico con el fichero java con lo que necesitas.
Saludos!
Saludos!
Lo he hecho muy deprisa y es un poco chapucilla, pero puede servirte de base.
import java.util.StringTokenizer;
public class EuroConversor
{
private static final String[] unidades=new String[30];
private static final String[] decenas=new String[10];
private static final String[] centenas=new String[10];
static
{
unidades[0]="";
unidades[1]="un";
unidades[2]="dos";
unidades[3]="tres";
unidades[4]="cuatro";
unidades[5]="cinco";
unidades[6]="seis";
unidades[7]="siete";
unidades[8]="ocho";
unidades[9]="nueve";
unidades[10]="diez";
unidades[11]="once";
unidades[12]="doce";
unidades[13]="trece";
unidades[14]="catorce";
unidades[15]="quince";
unidades[16]="dieciseis";
unidades[17]="diecisite";
unidades[18]="dieciocho";
unidades[19]="diecinueve";
unidades[20]="veinte";
unidades[21]="veintiuno";
unidades[22]="veintidos";
unidades[23]="veintitres";
unidades[24]="veinticuatro";
unidades[25]="veinticinco";
unidades[26]="veintiseis";
unidades[27]="veintisiete";
unidades[28]="veintiocho";
unidades[29]="veintinueve";
decenas[0]="";
decenas[1]="";
decenas[2]="";
decenas[3]="treinta";
decenas[4]="cuarenta";
decenas[5]="cincuenta";
decenas[6]="sesenta";
decenas[7]="setenta";
decenas[8]="ochenta";
decenas[9]="noventa";
centenas[0]="";
centenas[1]="cien";
centenas[2]="doscientos";
centenas[3]="trescientos";
centenas[4]="cuatrocientos";
centenas[5]="quinientos";
centenas[6]="seiscientos";
centenas[7]="setecientos";
centenas[8]="ochocientos";
centenas[9]="novecientos";
}
private EuroConversor() {}
public static String convertir (String euroCantidad) throws Exception
{
String euroCadena="";
String[] cantidad=new String[2];
StringTokenizer stk=new StringTokenizer(euroCantidad,".");
if (stk.countTokens() != 2) throw new Exception("Cantidad erronea");
int i=0;
while (stk.hasMoreTokens())
{
cantidad[i++]=stk.nextToken();
}
int longitudEntera=cantidad[0].length();
int longitudDecimal=cantidad[1].length();
if (longitudDecimal > 2) throw new Exception("Cantidad erronea. Demasiados decimales");
int f;
int g;
try
{
f=Integer.parseInt(cantidad[0]);
g=Integer.parseInt(cantidad[1]);
}
catch (NumberFormatException nfe) {throw new Exception("Cantidad erronea");}
if (f > 1000000 || (f == 1000000 && g > 0)) throw new Exception("Cantidad erronea. Supera el millon");
if (f == 1000000) return "un millon de euros";
String cantidadEntera="";
String cantidadDecimal="";
if (longitudEntera < 6)
{
for (int j=1; j <= 6-longitudEntera; j++)
{
cantidadEntera=cantidadEntera + "0";
}
cantidadEntera=cantidadEntera + cantidad[0];
}
else
{
cantidadEntera=cantidad[0];
}
if (longitudDecimal < 2)
{
cantidadDecimal=cantidad[1] + "0";
}
else
{
cantidadDecimal=cantidad[1];
}
try
{
//Parte entera
int centenasMillar=Integer.parseInt(cantidadEntera.substring(0,1));
int decenasMillar=Integer.parseInt(cantidadEntera.substring(1,2));
int unidadesMillar=Integer.parseInt(cantidadEntera.substring(2,3));
int sCentenas=Integer.parseInt(cantidadEntera.substring(3,4));
int sDecenas=Integer.parseInt(cantidadEntera.substring(4,5));
int sUnidades=Integer.parseInt(cantidadEntera.substring(5,6));
euroCadena=euroCadena + centenas[centenasMillar];
if (centenasMillar == 1 && (decenasMillar > 0 || unidadesMillar > 0))
{
euroCadena=euroCadena + "to";
}
if (decenasMillar < 3)
{
int indiceUnidades=Integer.parseInt(cantidadEntera.substring(1,3));
if (indiceUnidades > 0)
euroCadena=euroCadena.trim() + " " + unidades[indiceUnidades];
}
else
{
if (decenasMillar > 0) euroCadena=euroCadena.trim() + " " + decenas[decenasMillar];
if (unidadesMillar > 0)
{
euroCadena=euroCadena.trim() + " y " + unidades[unidadesMillar];
}
}
if (euroCadena.trim().length() > 0)
{
if (euroCadena.trim().equalsIgnoreCase("un"))
{
euroCadena="mil ";
}
else
{
euroCadena=euroCadena.trim() + " mil ";
}
}
euroCadena=euroCadena + centenas[sCentenas];
if (sCentenas == 1 && (sDecenas > 0 || sUnidades > 0))
{
euroCadena=euroCadena + "to";
}
if (sDecenas < 3)
{
int indiceUnidades=Integer.parseInt(cantidadEntera.substring(4,6));
if (indiceUnidades > 0) euroCadena=euroCadena.trim() + " " + unidades[indiceUnidades];
}
else
{
if (sDecenas > 0) euroCadena=euroCadena.trim() + " " + decenas[sDecenas];
if (sUnidades > 0)
{
euroCadena=euroCadena.trim() + " y " + unidades[sUnidades];
}
}
if (euroCadena.trim().length() > 0) {euroCadena=euroCadena.trim() + " euro";}
int valorEntero=Integer.parseInt(cantidadEntera);
if (valorEntero > 1) {euroCadena=euroCadena + "s";}
//Parte decimal
int decenasDecimal=Integer.parseInt(cantidadDecimal.substring(0,1));
int unidadesDecimal=Integer.parseInt(cantidadDecimal.substring(1,2));
if (decenasDecimal < 3)
{
int indiceUnidades=Integer.parseInt(cantidadDecimal.substring(0,2));
if (indiceUnidades > 0)
{
if (valorEntero > 0)
{
euroCadena=euroCadena.trim() + " con " + unidades[indiceUnidades] + " centimo";
}
else
{
euroCadena=euroCadena.trim() + " " + unidades[indiceUnidades] + " centimo";
}
}
}
else
{
if (valorEntero > 0)
{
euroCadena=euroCadena.trim() + " con " + decenas[decenasDecimal];
}
else
{
euroCadena=euroCadena.trim() + " " + decenas[decenasDecimal];
}
if (unidadesDecimal > 0)
{
euroCadena=euroCadena.trim() + " y " + unidades[unidadesDecimal] + " centimo";
}
else
{
euroCadena=euroCadena.trim() + " centimo";
}
}
int valorDecimal=Integer.parseInt(cantidadDecimal);
if (valorDecimal > 1) {euroCadena=euroCadena.trim() + "s";}
}
catch (Exception e1) {throw new Exception("Cantidad erronea 3");}
return euroCadena.trim();
}
}
import java.util.StringTokenizer;
public class EuroConversor
{
private static final String[] unidades=new String[30];
private static final String[] decenas=new String[10];
private static final String[] centenas=new String[10];
static
{
unidades[0]="";
unidades[1]="un";
unidades[2]="dos";
unidades[3]="tres";
unidades[4]="cuatro";
unidades[5]="cinco";
unidades[6]="seis";
unidades[7]="siete";
unidades[8]="ocho";
unidades[9]="nueve";
unidades[10]="diez";
unidades[11]="once";
unidades[12]="doce";
unidades[13]="trece";
unidades[14]="catorce";
unidades[15]="quince";
unidades[16]="dieciseis";
unidades[17]="diecisite";
unidades[18]="dieciocho";
unidades[19]="diecinueve";
unidades[20]="veinte";
unidades[21]="veintiuno";
unidades[22]="veintidos";
unidades[23]="veintitres";
unidades[24]="veinticuatro";
unidades[25]="veinticinco";
unidades[26]="veintiseis";
unidades[27]="veintisiete";
unidades[28]="veintiocho";
unidades[29]="veintinueve";
decenas[0]="";
decenas[1]="";
decenas[2]="";
decenas[3]="treinta";
decenas[4]="cuarenta";
decenas[5]="cincuenta";
decenas[6]="sesenta";
decenas[7]="setenta";
decenas[8]="ochenta";
decenas[9]="noventa";
centenas[0]="";
centenas[1]="cien";
centenas[2]="doscientos";
centenas[3]="trescientos";
centenas[4]="cuatrocientos";
centenas[5]="quinientos";
centenas[6]="seiscientos";
centenas[7]="setecientos";
centenas[8]="ochocientos";
centenas[9]="novecientos";
}
private EuroConversor() {}
public static String convertir (String euroCantidad) throws Exception
{
String euroCadena="";
String[] cantidad=new String[2];
StringTokenizer stk=new StringTokenizer(euroCantidad,".");
if (stk.countTokens() != 2) throw new Exception("Cantidad erronea");
int i=0;
while (stk.hasMoreTokens())
{
cantidad[i++]=stk.nextToken();
}
int longitudEntera=cantidad[0].length();
int longitudDecimal=cantidad[1].length();
if (longitudDecimal > 2) throw new Exception("Cantidad erronea. Demasiados decimales");
int f;
int g;
try
{
f=Integer.parseInt(cantidad[0]);
g=Integer.parseInt(cantidad[1]);
}
catch (NumberFormatException nfe) {throw new Exception("Cantidad erronea");}
if (f > 1000000 || (f == 1000000 && g > 0)) throw new Exception("Cantidad erronea. Supera el millon");
if (f == 1000000) return "un millon de euros";
String cantidadEntera="";
String cantidadDecimal="";
if (longitudEntera < 6)
{
for (int j=1; j <= 6-longitudEntera; j++)
{
cantidadEntera=cantidadEntera + "0";
}
cantidadEntera=cantidadEntera + cantidad[0];
}
else
{
cantidadEntera=cantidad[0];
}
if (longitudDecimal < 2)
{
cantidadDecimal=cantidad[1] + "0";
}
else
{
cantidadDecimal=cantidad[1];
}
try
{
//Parte entera
int centenasMillar=Integer.parseInt(cantidadEntera.substring(0,1));
int decenasMillar=Integer.parseInt(cantidadEntera.substring(1,2));
int unidadesMillar=Integer.parseInt(cantidadEntera.substring(2,3));
int sCentenas=Integer.parseInt(cantidadEntera.substring(3,4));
int sDecenas=Integer.parseInt(cantidadEntera.substring(4,5));
int sUnidades=Integer.parseInt(cantidadEntera.substring(5,6));
euroCadena=euroCadena + centenas[centenasMillar];
if (centenasMillar == 1 && (decenasMillar > 0 || unidadesMillar > 0))
{
euroCadena=euroCadena + "to";
}
if (decenasMillar < 3)
{
int indiceUnidades=Integer.parseInt(cantidadEntera.substring(1,3));
if (indiceUnidades > 0)
euroCadena=euroCadena.trim() + " " + unidades[indiceUnidades];
}
else
{
if (decenasMillar > 0) euroCadena=euroCadena.trim() + " " + decenas[decenasMillar];
if (unidadesMillar > 0)
{
euroCadena=euroCadena.trim() + " y " + unidades[unidadesMillar];
}
}
if (euroCadena.trim().length() > 0)
{
if (euroCadena.trim().equalsIgnoreCase("un"))
{
euroCadena="mil ";
}
else
{
euroCadena=euroCadena.trim() + " mil ";
}
}
euroCadena=euroCadena + centenas[sCentenas];
if (sCentenas == 1 && (sDecenas > 0 || sUnidades > 0))
{
euroCadena=euroCadena + "to";
}
if (sDecenas < 3)
{
int indiceUnidades=Integer.parseInt(cantidadEntera.substring(4,6));
if (indiceUnidades > 0) euroCadena=euroCadena.trim() + " " + unidades[indiceUnidades];
}
else
{
if (sDecenas > 0) euroCadena=euroCadena.trim() + " " + decenas[sDecenas];
if (sUnidades > 0)
{
euroCadena=euroCadena.trim() + " y " + unidades[sUnidades];
}
}
if (euroCadena.trim().length() > 0) {euroCadena=euroCadena.trim() + " euro";}
int valorEntero=Integer.parseInt(cantidadEntera);
if (valorEntero > 1) {euroCadena=euroCadena + "s";}
//Parte decimal
int decenasDecimal=Integer.parseInt(cantidadDecimal.substring(0,1));
int unidadesDecimal=Integer.parseInt(cantidadDecimal.substring(1,2));
if (decenasDecimal < 3)
{
int indiceUnidades=Integer.parseInt(cantidadDecimal.substring(0,2));
if (indiceUnidades > 0)
{
if (valorEntero > 0)
{
euroCadena=euroCadena.trim() + " con " + unidades[indiceUnidades] + " centimo";
}
else
{
euroCadena=euroCadena.trim() + " " + unidades[indiceUnidades] + " centimo";
}
}
}
else
{
if (valorEntero > 0)
{
euroCadena=euroCadena.trim() + " con " + decenas[decenasDecimal];
}
else
{
euroCadena=euroCadena.trim() + " " + decenas[decenasDecimal];
}
if (unidadesDecimal > 0)
{
euroCadena=euroCadena.trim() + " y " + unidades[unidadesDecimal] + " centimo";
}
else
{
euroCadena=euroCadena.trim() + " centimo";
}
}
int valorDecimal=Integer.parseInt(cantidadDecimal);
if (valorDecimal > 1) {euroCadena=euroCadena.trim() + "s";}
}
catch (Exception e1) {throw new Exception("Cantidad erronea 3");}
return euroCadena.trim();
}
}