Convertir un numero a su descripcion en letras
Recientemente tuve la necesidad de realizar la conversión de un numero en su descripción en letras o mejor dicho en su representación monetaria (PESO MEXICANO). Creo que el algoritmo se llama "Conversión del importe" o "Función del importe en letras" pero bueno la web habla tendido y largo de esto.
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
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
Ver el código en java en mi blog [ExplotandoCódigo Blog]
Saludos.
- gabo's blog
- Inicie sesión o regístrese para enviar comentarios
Comentarios
excelente realmente funciona
excelente realmente funciona jeje aunque deberias haber puesto el codigo aqui y una referencia a tu blog al pie del mismo
de esta manera tendrias mas comentarios sin que tengamos la obligacion de ir a tu blog ya que de alguna manera
este tambien es tu blog... genial
Interesante
Buena clase, yo tambien voy a tener que hacer esta clase para un sistema de ventas, pero ya que lo publicas
puedo usarlo? o tendre que quebrarme la cabeza un rato jejejjeejej
Saludos
versión no recursiva
Hay una manera no recursiva de hacer esta función, para no atascar el stack cuando quieres convertir números muy grandes, y utilizando un StringBuilder para ir pegando ahi los numeros que vas sacando; requiere que tengas un arreglo con los fragmentos de números, decenas, centenas, y los casos especiales para "un mil", "cien" (el 100 exacto contra ciento y tantos), etc.
Given the choice of dancing pigs and security, users will choose dancing pigs, every single time. - Steve Riley
Por supuesto
Es bien sabido que
StringBuilder
es mas rapido queStringBuffer
, vaya que se puede mejorar el algoritmo.Gabriel Martínez. Nájera
Coatzacoalcos, Veracruz México