Results 1 to 2 of 2

Thread: convert numbers to words (SPANISH)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    convert numbers to words (SPANISH)

    Here is a function to convert an integer to Spanish words. Maybe it will be of some use to somebody. There is probably a much more elegant way to do this but this worked for me and is correct as far as I can see.

    Code:
    Private Function ConvertToWords(ByRef Value As Integer) As String
    Dim Strng As String     'holds string representation of Value
    Strng = CStr(Value)
    Dim Txt As String       'holds the word string as it is being built
    Dim Curr As String      'holds the currency word
    Curr = "Euros"
    Dim Temp As String     'used to work on a single digit at a time
    
    Dim Units(12) As String
    Units(0) = "zero"
    Units(1) = "uno"
    Units(2) = "dos"
    Units(3) = "tres"
    Units(4) = "cuatro"
    Units(5) = "cinco"
    Units(6) = "seis"
    Units(7) = "siete"
    Units(8) = "ocho"
    Units(9) = "nueve"
    Units(10) = "nove"
    Units(11) = "sete"
    Units(12) = "quin"
    
    Dim Tens(9) As String
    Tens(0) = ""
    Tens(1) = "diez"
    Tens(2) = "veinte"
    Tens(3) = "treinta"
    Tens(4) = "cuarenta"
    Tens(5) = "cincuenta"
    Tens(6) = "sesenta"
    Tens(7) = "setenta"
    Tens(8) = "ochenta"
    Tens(9) = "noventa"
    
    Dim Teens(5)
    Teens(1) = "once"
    Teens(2) = "doce"
    Teens(3) = "trece"
    Teens(4) = "catorce"
    Teens(5) = "quince"
    
       Txt = ""
    'hundreds
       If Len(Strng) = 3 Then  'there are hundreds
          Temp = Left(Right(Strng, 3), 1)     'take the "hundreds" digit
          If Temp = "5" Then Temp = "12"      'set stem for 500s as "quin"
          If Temp = "7" Then Temp = "11"      'account for siete-sete change in 700's
          If Temp = "9" Then Temp = "10"      'account for nueve-nove change in 900's
          Txt = Units(CInt(Temp))             'set the hundreds word according to units list
          If Temp = "1" Then                  'the 100's is a special case. Only say "hundred" not "one hundred"
             If Right(Strng, 2) = "00" Then   'also exactly 100 is a special case.
                Txt = "Cien"
             Else
                Txt = "ciento"                'not exactly 100 so add correct "hundreds" word
             End If
          Else
             If Temp = "12" Then
                Txt = Txt & "ientos"          'add correct ending for 500's
             Else
                Txt = Txt & "cientos"            'more than 100 so make 200,300,400 etc
             End If
          End If
       End If
    'tens
       If Len(Strng) <= 3 Then                'ignore numbers over 999
          Temp = Right(Strng, 2)              'take the tens and units
          If Temp >= "11" And Temp <= "15" Then
             Txt = Txt & " " & Teens(CInt(Temp) - 10) '11 to 15 are special words. Take them from Teens array
          Else
             'tens
             If Len(Temp) > 1 Then            'take the tens digit
                Txt = Txt & " " & Tens(CInt(Left(Temp, 1)))  'and add it to the word list
             End If
             'units
             If Len(Temp) > 1 Then            'deal if the value is 10 or more
                If CInt(Right(Temp, 1)) <> "0" Then
                   If CInt(Left(Temp, 1)) = "1" Then     'if the tens begin with "1" need to change the spelling
                      Txt = Left(Txt, Len(Txt) - 1) & "ci" & Units(CInt(Right(Temp, 1)))
                   ElseIf CInt(Left(Temp, 1)) = "2" Then  'if tens begins with "2" change spelling differently
                      Txt = Left(Txt, Len(Txt) - 1) & "i" & Units(CInt(Right(Temp, 1)))
                   Else
                      If Left(Temp, 1) = "0" Then   'check for whole 10's
                         Txt = Txt & Units(CInt(Right(Temp, 1)))
                      Else
                         Txt = Txt & " y " & Units(CInt(Right(Temp, 1))) 'if not divisible by 10, add the units
                      End If
                   End If
                End If
             Else
                Txt = Txt & Units(CInt(Temp))       'this adds units to the hundreds
             End If
          End If
       End If
       If Temp = "1" Then
          Txt = Txt & " " & Left(Curr, Len(Curr) - 1)  'remove the "s" for just a single Euro
       Else
          Txt = Txt & " " & Curr
       End If
       Txt = Trim(Txt)         'remove surplus spaces before and after string
       Txt = UCase(Left(Txt, 1)) & Right(Txt, (Len(Txt) - 1))   'capitalise the first letter
       ConvertToWords = Txt    'return the string
    End Function

  2. #2
    New Member
    Join Date
    Dec 2013
    Posts
    1

    Re: convert numbers to words (SPANISH)

    It converts numbers up to 999, just integers...

    If you intend to use it to convert currency, you should do a calling function:

    1) group the integer part in groups of three digits and call the function each time, adding at the end "euro"/"euros", "mil", "millón", "billón" as needed
    2) call the function with the fractional part, rounding to 2 digits

    Add the parameter "strCurrency" to the ConvertToWords function, so you can use Euros, Cents, Pounds, Dollars or whatever

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width