VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Converts Numbers to words in Spanish

by Guillermo Marinero (1 Submission)
Category: Miscellaneous
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Tue 7th September 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Converts Numbers to words in Spanish

Rate Converts Numbers to words in Spanish



Public Function Num2String(Qty As String) As String
On Error GoTo flgerr
'Numero a string
Qty = Format(Qty, "####0.00")
Dim temp As String
    If InStr(1, Qty, ".") > 0 Then
        temp = Right$(Qty, Len(Qty) - InStr(1, Qty, "."))
        Qty = Fix(Qty)
    End If
    Select Case Len(Qty)
        Case 1
            Num2String = Unidades(Qty, True)
        Case 2
            Num2String = Decenas(Qty)
        Case 3
            Num2String = Centenas(Qty)
        Case 4
            Num2String = UniMiles(Qty)
        Case 5
            Num2String = DecMiles(Qty)
        Case 6
            Num2String = CenMiles(Qty)
        Case 7
            Num2String = UniMillon(Qty)
    End Select
    
    If Len(temp) > 0 Then
        Num2String = Num2String + Space(1) + temp + "/100"
    End If
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function
Private Function Unidades(Qty As String, Optional Un As Boolean) As String
On Error GoTo flgerr
    Unidades = Choose(CLng(Qty) + 1, "Cero", IIf(Un, "Un", "Uno"), "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete", "Ocho", "Nueve")
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function

Private Function Decenas(Qty As String) As String
On Error GoTo flgerr
    Select Case CInt(Qty)
        Case 0
            Decenas = vbNullString
        Case 10
            Decenas = "Diez"
        Case 11
            Decenas = "Once"
        Case 12
            Decenas = "Doce"
        Case 13
            Decenas = "Trece"
        Case 14
            Decenas = "Catorce"
        Case 15
            Decenas = "Quince"
        Case 16
            Decenas = "Diez y Seis"
        Case 17
            Decenas = "Diez y Siete"
        Case 18
            Decenas = "Diez y Ocho"
        Case 19
            Decenas = "Diez y Nueve"
        Case 20
            Decenas = "Veinte"
        Case 21
            Decenas = "Veintiun"
        Case 22
            Decenas = "Veintidos"
        Case 23
            Decenas = "Veintitres"
        Case 24
            Decenas = "Veinticuatro"
        Case 25
            Decenas = "Veinticinco"
        Case 26
            Decenas = "Veintiseis"
        Case 27
            Decenas = "Veintisiete"
        Case 28
            Decenas = "Veintiocho"
        Case 20
            Decenas = "Veintinueve"
        Case 30
            Decenas = "Treinta"
        Case 40
            Decenas = "Cuarenta"
        Case 50
            Decenas = "Cincuenta"
        Case 60
            Decenas = "Sesenta"
        Case 70
            Decenas = "Setenta"
        Case 80
            Decenas = "Ochenta"
        Case 90
            Decenas = "Noventa"
        Case Else
            Decenas = Decenas(Left$(Qty, 1) * 10) & IIf(Left$(Qty, 1) = 0, "", Space(1) & "y" + Space(1)) & Unidades(Right$(Qty, 1), True)
    End Select
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function

Private Function Centenas(Qty As String) As String
On Error GoTo flgerr
    Select Case Left$(Qty, 1)
        Case 1
            If Qty = "100" Then: Centenas = "Cien": Else: Centenas = "Ciento"
        Case 2
            Centenas = "Doscientos"
        Case 3
            Centenas = "Trescientos"
        Case 4
            Centenas = "Cuatrocientos"
        Case 5
            Centenas = "Quinientos"
        Case 6
            Centenas = "Seiscientos"
        Case 7
            Centenas = "Setecientos"
        Case 8
            Centenas = "Ochocientos"
        Case 9
            Centenas = "Novecientos"
    End Select
    Centenas = Trim$(Centenas + Space(1) + Trim$(Decenas(Right$(Qty, 2))))
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function

Private Function UniMiles(Qty As String) As String
On Error GoTo flgerr
    UniMiles = Trim$(Unidades(Left$(Qty, 1), True) + " Mil" + Space(1) + Trim$(Centenas(Right$(Qty, 3))))
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function

Private Function DecMiles(Qty As String) As String
On Error GoTo flgerr
    DecMiles = Trim$(Decenas(Left$(Qty, 2)) + " Mil " + Centenas(Right$(Qty, 3)))
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function

Private Function CenMiles(Qty As String) As String
On Error GoTo flgerr
    CenMiles = Trim$(IIf(Len(Centenas(Left$(Qty, 3))) = 0, Centenas(Right$(Qty, 3)), Centenas(Left$(Qty, 3)) + " Mil " + Centenas(Right$(Qty, 3))))
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function

Private Function UniMillon(Qty As String)
On Error GoTo flgerr
    UniMillon = Unidades(Left$(Qty, 1), IIf(Left$(Qty, 1) = 1, True, False)) + IIf(Left$(Qty, 1) = 1, " Millon ", " Millones ") + CenMiles(Right$(Qty, 6))
Exit Function
flgerr:
    ErrTrap "miscelaneous", Err.Description, Err.Number
End Function



Download this snippet    Add to My Saved Code

Converts Numbers to words in Spanish Comments

No comments have been posted about Converts Numbers to words in Spanish. Why not be the first to post a comment about Converts Numbers to words in Spanish.

Post your comment

Subject:
Message:
0/1000 characters