Recientemente tuve la necesidad de realizar la conversión de un numero en su descripción en letras o mejor dicho en pesos mexicanos. Creo que el algoritmo se llama "Conversión del importe" o "Función del importe en letras" y la web habla tendido y largo de esto.
* Para los que programan en Visual Basic
Me llamo la atención este código hecho en Visual Basic, el cual adapte a Java
Public Function Num2Text(ByVal value As Double) As String
Select Case value
Case 0 : Num2Text = "CERO"
Case 1 : Num2Text = "UN"
Case 2 : Num2Text = "DOS"
Case 3 : Num2Text = "TRES"
Case 4 : Num2Text = "CUATRO"
Case 5 : Num2Text = "CINCO"
Case 6 : Num2Text = "SEIS"
Case 7 : Num2Text = "SIETE"
Case 8 : Num2Text = "OCHO"
Case 9 : Num2Text = "NUEVE"
Case 10 : Num2Text = "DIEZ"
Case 11 : Num2Text = "ONCE"
Case 12 : Num2Text = "DOCE"
Case 13 : Num2Text = "TRECE"
Case 14 : Num2Text = "CATORCE"
Case 15 : Num2Text = "QUINCE"
Case Is < 20 : Num2Text = "DIECI" & Num2Text(value - 10)
Case 20 : Num2Text = "VEINTE"
Case Is < 30 : Num2Text = "VEINTI" & Num2Text(value - 20)
Case 30 : Num2Text = "TREINTA"
Case 40 : Num2Text = "CUARENTA"
Case 50 : Num2Text = "CINCUENTA"
Case 60 : Num2Text = "SESENTA"
Case 70 : Num2Text = "SETENTA"
Case 80 : Num2Text = "OCHENTA"
Case 90 : Num2Text = "NOVENTA"
Case Is < 100 : Num2Text = Num2Text(Int(value \ 10) * 10) & " Y " & Num2Text(value Mod 10)
Case 100 : Num2Text = "CIEN"
Case Is < 200 : Num2Text = "CIENTO " & Num2Text(value - 100)
Case 200, 300, 400, 600, 800 : Num2Text = Num2Text(Int(value \ 100)) & "CIENTOS"
Case 500 : Num2Text = "QUINIENTOS"
Case 700 : Num2Text = "SETECIENTOS"
Case 900 : Num2Text = "NOVECIENTOS"
Case Is < 1000 : Num2Text = Num2Text(Int(value \ 100) * 100) & " " & Num2Text(value Mod 100)
Case 1000 : Num2Text = "MIL"
Case Is < 2000 : Num2Text = "MIL " & Num2Text(value Mod 1000)
Case Is < 1000000 : Num2Text = Num2Text(Int(value \ 1000)) & " MIL"
If value Mod 1000 Then Num2Text = Num2Text & " " & Num2Text(value Mod 1000)
Case 1000000 : Num2Text = "UN MILLON"
Case Is < 2000000 : Num2Text = "UN MILLON " & Num2Text(value Mod 1000000)
Case Is < 1000000000000.0# : Num2Text = Num2Text(Int(value / 1000000)) & " MILLONES "
If (value - Int(value / 1000000) * 1000000) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000) * 1000000)
Case 1000000000000.0# : Num2Text = "UN BILLON"
Case Is < 2000000000000.0# : Num2Text = "UN BILLON " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
Case Else : Num2Text = Num2Text(Int(value / 1000000000000.0#)) & " BILLONES"
If (value - Int(value / 1000000000000.0#) * 1000000000000.0#) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
End Select
End Function
* Para lo que usen Crystal report, ya tienen la solución en la mesa
* Hay algoritmos que sobrepasan el numero 999 999 999
* Esta fue mi propuesta por supuesto que no es una solución definitiva (Considerando que los números que usaré no son muy grandes)
/**
* @author Gabriel Mtz.
*/
public class Converter {
Integer counter=0;
String value="";
public String getStringOfNumber(Integer $num){
this.counter = $num;
return doThings($num);
}
private String doThings(Integer _counter){
//Limite
if(_counter >2000000)
return "DOS MILLONES";
switch(_counter){
case 0: return "CERO";
case 1: return "UN"; //UNO
case 2: return "DOS";
case 3: return "TRES";
case 4: return "CUATRO";
case 5: return "CINCO";
case 6: return "SEIS";
case 7: return "SIETE";
case 8: return "OCHO";
case 9: return "NUEVE";
case 10: return "DIEZ";
case 11: return "ONCE";
case 12: return "DOCE";
case 13: return "TRECE";
case 14: return "CATORCE";
case 15: return "QUINCE";
case 20: return "VEINTE";
case 30: return "TREINTA";
case 40: return "CUARENTA";
case 50: return "CINCUENTA";
case 60: return "SESENTA";
case 70: return "SETENTA";
case 80: return "OCHENTA";
case 90: return "NOVENTA";
case 100: return "CIEN";
case 200: return "DOSCIENTOS";
case 300: return "TRESCIENTOS";
case 400: return "CUATROCIENTOS";
case 500: return "QUINIENTOS";
case 600: return "SEISCIENTOS";
case 700: return "SETECIENTOS";
case 800: return "OCHOCIENTOS";
case 900: return "NOVECIENTOS";
case 1000: return "MIL";
case 1000000: return "UN MILLON";
case 2000000: return "DOS MILLONES";
}
if(_counter<20){
//System.out.println(">15");
return "DIECI"+ doThings(_counter-10);
}
if(_counter<30){
//System.out.println(">20");
return "VEINTI" + doThings(_counter-20);
}
if(_counter<100){
//System.out.println("<100");
return doThings( (int)(_counter/10)*10 ) + " Y " + doThings(_counter%10);
}
if(_counter<200){
//System.out.println("<200");
return "CIENTO " + doThings( _counter - 100 );
}
if(_counter<1000){
//System.out.println("<1000");
return doThings( (int)(_counter/100)*100 ) + " " + doThings(_counter%100);
}
if(_counter<2000){
//System.out.println("<2000");
return "MIL " + doThings( _counter % 1000 );
}
if(_counter<1000000){
String var="";
//System.out.println("<1000000");
var = doThings((int)(_counter/1000)) + " MIL" ;
if(_counter % 1000!=0){
//System.out.println(var);
var += " " + doThings(_counter % 1000);
}
return var;
}
if(_counter<2000000){
return "UN MILLON " + doThings( _counter % 1000000 );
}
return "";
}
}
El resto depende de lo que quieran lograr con este algoritmo. Podrían adaptarlo para que sea especifico a un Locale, Moneda o tal vez para cierto idioma =)
