Clase para convertir un número en su literal

Literal es una clase para Visual Basic que sirve para obtener el literal de un número. Por ejemplo, el literal de 23456 es "Veintitres mil cuatrocientos cintuenta y seis". La versión actual de "Literal" admite números del 1 al 999999. Más información en: http://www.mhandal.net/source/vb/literal/index.php
				VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "Literal"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private m_sUltimoError As String

Private Function fnUnidades(ByVal nNumero As Long, Optional ByVal bMl As Boolean = False) As String

Select Case nNumero
Case 1
If bMl Then
fnUnidades = "un"
Else
fnUnidades = "uno"
End If
Case 2
fnUnidades = "dos"
Case 3
fnUnidades = "tres"
Case 4
fnUnidades = "cuatro"
Case 5
fnUnidades = "cinco"
Case 6
fnUnidades = "seis"
Case 7
fnUnidades = "siete"
Case 8
fnUnidades = "ocho"
Case 9
fnUnidades = "nueve"
End Select

End Function

Private Function fnDecenas(ByVal nNumero As Long, Optional ByVal bMl As Boolean = False) As String

If nNumero < 10 Then
fnDecenas = fnUnidades(nNumero)
ElseIf nNumero = 10 Then
fnDecenas = "diez"
ElseIf nNumero = 11 Then
fnDecenas = "once"
ElseIf nNumero = 12 Then
fnDecenas = "doce"
ElseIf nNumero = 13 Then
fnDecenas = "trece"
ElseIf nNumero = 14 Then
fnDecenas = "catorce"
ElseIf nNumero = 15 Then
fnDecenas = "quince"
ElseIf nNumero >= 16 And nNumero < 20 Then
fnDecenas = "diez y " & fnUnidades(nNumero - 10)
ElseIf nNumero >= 20 And nNumero < 30 Then
fnDecenas = "veint" & IIf((nNumero - 20) = 0, "e", "i" & fnUnidades(nNumero - 20, bMl))
ElseIf nNumero >= 30 And nNumero < 40 Then
fnDecenas = "treinta" & IIf((nNumero - 30) = 0, "", " y " & fnUnidades(nNumero - 30, bMl))
ElseIf nNumero >= 40 And nNumero < 50 Then
fnDecenas = "cuarenta" & IIf((nNumero - 40) = 0, "", " y " & fnUnidades(nNumero - 40, bMl))
ElseIf nNumero >= 50 And nNumero < 60 Then
fnDecenas = "cincuenta" & IIf((nNumero - 50) = 0, "", " y " & fnUnidades(nNumero - 50, bMl))
ElseIf nNumero >= 60 And nNumero < 70 Then
fnDecenas = "sesenta" & IIf((nNumero - 60) = 0, "", " y " & fnUnidades(nNumero - 60, bMl))
ElseIf nNumero >= 70 And nNumero < 80 Then
fnDecenas = "setenta" & IIf((nNumero - 70) = 0, "", " y " & fnUnidades(nNumero - 70, bMl))
ElseIf nNumero >= 80 And nNumero < 90 Then
fnDecenas = "ochenta" & IIf((nNumero - 80) = 0, "", " y " & fnUnidades(nNumero - 80, bMl))
ElseIf nNumero >= 90 And nNumero < 100 Then
fnDecenas = "noventa" & IIf((nNumero - 90) = 0, "", " y " & fnUnidades(nNumero - 90, bMl))
End If

End Function

Private Function fnCentenas(ByVal nNumero As Long, Optional ByVal bMl As Boolean = False) As String

If nNumero < 100 Then
fnCentenas = fnDecenas(nNumero, bMl)
ElseIf nNumero >= 100 And nNumero < 200 Then
fnCentenas = "cien" & IIf((nNumero - 100) = 0, "", "to " & fnDecenas(nNumero - 100, bMl))
ElseIf nNumero >= 200 And nNumero < 300 Then
fnCentenas = "doscientos" & IIf((nNumero - 200) = 0, "", " " & fnDecenas(nNumero - 200, bMl))
ElseIf nNumero >= 300 And nNumero < 400 Then
fnCentenas = "trecientos" & IIf((nNumero - 300) = 0, "", " " & fnDecenas(nNumero - 300, bMl))
ElseIf nNumero >= 400 And nNumero < 500 Then
fnCentenas = "cuatrocientos" & IIf((nNumero - 400) = 0, "", " " & fnDecenas(nNumero - 400, bMl))
ElseIf nNumero >= 500 And nNumero < 600 Then
fnCentenas = "quinientos" & IIf((nNumero - 500) = 0, "", " " & fnDecenas(nNumero - 500, bMl))
ElseIf nNumero >= 600 And nNumero < 700 Then
fnCentenas = "seiscientos" & IIf((nNumero - 600) = 0, "", " " & fnDecenas(nNumero - 600, bMl))
ElseIf nNumero >= 700 And nNumero < 800 Then
fnCentenas = "setecientos" & IIf((nNumero - 700) = 0, "", " " & fnDecenas(nNumero - 700, bMl))
ElseIf nNumero >= 800 And nNumero < 900 Then
fnCentenas = "ochocientos" & IIf((nNumero - 800) = 0, "", " " & fnDecenas(nNumero - 800, bMl))
ElseIf nNumero >= 900 And nNumero < 1000 Then
fnCentenas = "novecientos" & IIf((nNumero - 900) = 0, "", " " & fnDecenas(nNumero - 900, bMl))
End If

End Function

Private Function fnMillares(ByVal nNumero As Long) As String
If nNumero < 1000 Then
fnMillares = fnCentenas(nNumero)
ElseIf nNumero >= 1000 And nNumero < 2000 Then
fnMillares = "mil " & fnCentenas(nNumero - Int(nNumero / 1000) * 1000)
Else
fnMillares = fnCentenas(Int(nNumero / 1000), True) & " mil " & fnCentenas(nNumero - Int(nNumero / 1000) * 1000)
End If
End Function

Public Function UltimoError() As String
UltimoError = m_sUltimoError
End Function

Public Function Convertir(ByVal lNumero As Long) As String
On Error GoTo FuncErr

Dim sLiteral As String

'Reseteamos el último error
m_sUltimoError = ""

'Revisamos el rango
If lNumero >= 1 And lNumero <= 999999 Then

'Conseguimos el literal
sLiteral = fnMillares(lNumero)

'Cambiamos la primera letra a mayúscula
Convertir = Trim(UCase(Left(sLiteral, 1)) & Mid(sLiteral, 2))


Else
'Fijamos el último error
m_sUltimoError = "Número fuera del rango (1 a 999999)."
End If

Exit Function
FuncErr:
m_sUltimoError = Err.Description
End Function
Descargar adjuntos
COMPARTE ESTE TUTORIAL

COMPARTIR EN FACEBOOK
COMPARTIR EN TWITTER
COMPARTIR EN LINKEDIN
COMPARTIR EN WHATSAPP
TUTORIAL ANTERIOR

SIGUIENTE TUTORIAL