Você está na página 1de 7

FUNCION PARA CONVERTIR UNA CANTIDAD DE NUMERO A LETRA.

Option Explicit

'Funci�n principal

Function SpellNumber(ByVal MyNumber)

Dim Pesos, Centavos, Temp

Dim DecimalPlace, Count

ReDim Place(9) As String

Place(2) = " CIEN "

Place(3) = " MILLON "

Place(4) = " BILLON "

Place(5) = " TRILLONES "

' Representaci�n de cadena del importe.

MyNumber = Trim(Str(MyNumber))

' Posici�n de decimal 0 si no hay ninguno.

DecimalPlace = InStr(MyNumber, ".")

' Convertir c�ntimos y establecer MyNumber en cantidad de d�lares.

If DecimalPlace > 0 Then

Centavos = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _ "00", 2))

MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))

End If

Count = 1

Do While MyNumber <> ""

Temp = GetHundreds(Right(MyNumber, 3))

If Temp <> "" Then Pesos = Temp & Place(Count) & Pesos

If Len(MyNumber) > 3 Then

MyNumber = Left(MyNumber, Len(MyNumber) - 3)

Else

MyNumber = ""

End If
Count = Count + 1

Loop

Select Case Pesos

Case ""

Pesos = "No Pesos"

Case "One"

Pesos = "UN PESO"

Case Else

Pesos = Pesos & " PESOS"

End Select

Select Case Centavos

Case ""

Cents = " y No Centavos"

Case "One"

Centavos = " y UN CENTAVO"

Case Else

Cents = " Y " & Centavos & " CENTAVOS"

End Select

SpellNumber = Pesos & Centavos

End Function

' Convierte un n�mero entre 100 y 999 en texto

Function GetHundreds(ByVal MyNumber)

Dim Result As String

If Val(MyNumber) = 0 Then Exit Function

MyNumber = Right("000" & MyNumber, 3)

' Convertir la posici�n de las centenas.

If Mid(MyNumber, 1, 1) <> "0" Then

Result = GetDigit(Mid(MyNumber, 1, 1)) & " CIENTOS "

End If

' Convertir la posici�n de las decenas y unidades.


If Mid(MyNumber, 2, 1) <> "0" Then

Result = Result & GetTens(Mid(MyNumber, 2))

Else

Result = Result & GetDigit(Mid(MyNumber, 3))

End If

GetHundreds = Result

End Function

' Convierte un n�mero entre 10 y 99 en texto

Function GetTens(TensText)

Dim Result As String

Result = "" ' Convertir en NULL el valor de funci�n temporal.

If Val(Left(TensText, 1)) = 1 Then ' Si el valor est� comprendido entre 10-19...

Select Case Val(TensText)

Case 10: Result = "DIEZ"

Case 11: Result = "ONCE"

Case 12: Result = "DOCE"

Case 13: Result = "TRECE"

Case 14: Result = "CATORCE"

Case 15: Result = "QUINCE"

Case 16: Result = "DIECIS�IS"

Case 17: Result = "DIECISIETE"

Case 18: Result = "DIECIOCHO"

Case 19: Result = "DIECINUEVE"

Case Else

End Select

Else ' Si el valor est� entre 20 y 99...

Select Case Val(Left(TensText, 1))

Case 2: Result = "VEINTE "

Case 3: Result = "TREINTA "


Case 4: Result = "CUARENTA "

Case 5: Result = "CINCUENTA "

Case 6: Result = "SESENTA "

Case 7: Result = "SETENTA "

Case 8: Result = "OCHENTA "

Case 9: Result = "NOVENTA "

Case Else

End Select

Result = Result & GetDigit _

(Right(TensText, 1)) ' Recupera una posici�n.

End If

GetTens = Result

End Function

' Convierte un n�mero entre 1 y 9 en texto.

Function GetDigit(Digit)

Select Case Val(Digit)

Case 1: GetDigit = "UNO"

Case 2: GetDigit = "DOS"

Case 3: GetDigit = "TRES"

Case 4: GetDigit = "CUATRO"

Case 5: GetDigit = "CINCO"

Case 6: GetDigit = "SEIS"

Case 7: GetDigit = "SIETE"

Case 8: GetDigit = "OCHO"

Case 9: GetDigit = "NUEVE"

Case Else: GetDigit = ""

End Select

End Function

***********************************************************************************
***********************************
Function CONVERTIRNUM(Numero As Double, Optional CentimosEnLetra As Boolean) As
String

Dim Moneda As String


Dim Monedas As String
Dim Centimo As String
Dim Centimos As String
Dim Preposicion As String
Dim NumCentimos As Double
Dim Letra As String
Const Maximo = 1999999999.99

'************************************************************
' Par�metros
'************************************************************
Moneda = "Peso" 'Nombre de Moneda (Singular)
Monedas = "Pesos" 'Nombre de Moneda (Plural)
Centimo = "Centavo" 'Nombre de C�ntimos (Singular)
Centimos = "Centavos" 'Nombre de C�ntimos (Plural)
Preposicion = "Con" 'Preposici�n entre Moneda y C�ntimos
'************************************************************

'Validar que el Numero est� dentro de los l�mites


If (Numero >= 0) And (Numero <= Maximo) Then

Letra = NUMERORECURSIVO((Fix(Numero))) 'Convertir el Numero en


letras

'Si Numero = 1 agregar leyenda Moneda (Singular)


If (Numero = 1) Then
Letra = Letra & " " & Moneda
'De lo contrario agregar leyenda Monedas (Plural)
Else
Letra = Letra & " " & Monedas
End If

NumCentimos = Round((Numero - Fix(Numero)) * 100) 'Obtener los centimos del


Numero

'Si NumCentimos es mayor a cero inicar la conversi�n


If NumCentimos >= 0 Then
'Si el par�metro CentimosEnLetra es VERDADERO obtener letras para los
c�ntimos
If CentimosEnLetra Then
Letra = Letra & " " & Preposicion & " " &
NUMERORECURSIVO(Fix(NumCentimos)) 'Convertir los c�ntimos en letra

'Si NumCentimos = 1 agregar leyenda Centimos (Singular)


If (NumCentimos = 1) Then
Letra = Letra & " " & Centimo
'De lo contrario agregar leyenda Centimos (Plural)
Else
Letra = Letra & " " & Centimos
End If
'De lo contrario mostrar los c�ntimos como n�mero
Else
If NumCentimos < 10 Then
Letra = Letra & " 0" & NumCentimos & "/100 M.N."
Else
Letra = Letra & " " & NumCentimos & "/100 M.N."
End If
End If
End If

'Regresar el resultado final de la conversi�n


CONVERTIRNUM = Letra

Else
'Si el Numero no est� dentro de los l�mites, entivar un mensaje de error
CONVERTIRNUM = "ERROR: El n�mero excede los l�mites."
End If

End Function

Function NUMERORECURSIVO(Numero As Long) As String

Dim Unidades, Decenas, Centenas


Dim Resultado As String

'**************************************************
' Nombre de los n�meros
'**************************************************
Unidades = Array("", "Un", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete",
"Ocho", "Nueve", "Diez", "Once", "Doce", "Trece", "Catorce", "Quince", "Diecis�is",
"Diecisiete", "Dieciocho", "Diecinueve", "Veinte", "Veintiuno", "Veintidos",
"Veintitres", "Veinticuatro", "Veinticinco", "Veintiseis", "Veintisiete",
"Veintiocho", "Veintinueve")
Decenas = Array("", "Diez", "Veinte", "Treinta", "Cuarenta", "Cincuenta",
"Sesenta", "Setenta", "Ochenta", "Noventa", "Cien")
Centenas = Array("", "Ciento", "Doscientos", "Trescientos", "Cuatrocientos",
"Quinientos", "Seiscientos", "Setecientos", "Ochocientos", "Novecientos")
'**************************************************

Select Case Numero


Case 0
Resultado = "Cero"
Case 1 To 29
Resultado = Unidades(Numero)
Case 30 To 100
Resultado = Decenas(Numero \ 10) + IIf(Numero Mod 10 <> 0, " y " +
NUMERORECURSIVO(Numero Mod 10), "")
Case 101 To 999
Resultado = Centenas(Numero \ 100) + IIf(Numero Mod 100 <> 0, " " +
NUMERORECURSIVO(Numero Mod 100), "")
Case 1000 To 1999
Resultado = "Mil" + IIf(Numero Mod 1000 <> 0, " " + NUMERORECURSIVO(Numero
Mod 1000), "")
Case 2000 To 999999
Resultado = NUMERORECURSIVO(Numero \ 1000) + " Mil" + IIf(Numero Mod 1000
<> 0, " " + NUMERORECURSIVO(Numero Mod 1000), "")
Case 1000000 To 1999999
Resultado = "Un Mill�n" + IIf(Numero Mod 1000000 <> 0, " " +
NUMERORECURSIVO(Numero Mod 1000000), "")
Case 2000000 To 1999999999
Resultado = NUMERORECURSIVO(Numero \ 1000000) + " Millones" + IIf(Numero
Mod 1000000 <> 0, " " + NUMERORECURSIVO(Numero Mod 1000000), "")
End Select

NUMERORECURSIVO = Resultado

End Function

Você também pode gostar