Link to home
Start Free TrialLog in
Avatar of LSILes
LSILes

asked on

Converting numbers to actual words...

Does anyone know if there is a predefined function for converting numbers to words?  For example:  12 to "twelve"; 14,321 to "fourteen thousand three hundred twenty-one"; 1,300,000 to "one million three hundred thousand" and so forth?  If you know of one, or even have a predefined one, please let me know.  Thanks!
Avatar of Acid_Buzz
Acid_Buzz

I think no function make that you have to code it.. it's not verry hard to code it.. but is long..
I am sure there's a dll in english that will do that. Look for CHECK Writers dll or something like that. I wrote my own dll but sorry It's in portuguese language. anyway I will post something that you can understand, I use this function in a telephony application ( it plays wav files) so check and try it...
I will translate the code and post it again as soon as I have some time..

CODE----------------\/-------------
Public Function PlayNumber$(Number As Variant, Optional PlayType As ivrPlayType, Optional Decimals As Integer)

   ' Vamos formatar o número da forma:
   ' trilhão
   '  |    _bilhão           _décimo
   '  |   |    _milhão      | _centésimo
   '  |   |   |    _mil     || _milésimo
   '  |   |   |   |  cdu    |||
   ' 000.000.000.000.000 [, 000]
   
   Dim Intg$, Decm$, cdu$, du$, Res$
   Dim Casas%
   Dim i%
   
   ' Money has only 2 caracters
   If PlayType = Money Then
      Decimals = 2
   ' Limite até milésimo
   ElseIf Decimals > 3 Then
      Decimals = 3
   End If
   
   ' Parte inteira (até trilhão)
   Intg$ = Format$(CDbl(Fix(Number)), "000000000000")
   ' Parte decimal (elimitar zeros à direita)
   If Decimals And CDbl(Number) - Fix(CDbl(Number)) > 0 Then
      Decm$ = Format$(CDbl(Number) - Fix(CDbl(Number)), "." & String$(Decimals, "0"))
   End If
   Debug.Print "Number=" & Intg$ & Decm$
   ' Número de casas
   Casas = (Len(Intg$) \ 3) - 1
   Debug.Print "Casas=" & Casas
   
   ' Main loop
   For i = 1 To Len(Intg$) Step 3
     
      ' Centena, Dezena e Unidade
      cdu$ = Mid$(Intg$, i, 3)
      Debug.Print "cdu$=" & cdu$
      ' Dezena e Unidade
      du$ = Mid$(cdu$, 2)
      ' Verifica feminino
      If PlayType = Female Then
         ' é maior que 10?
         If Left$(du$, 1) <> "1" Then
            ' é uma ou duas?
            If Right$(du$, 1) = "1" Or Right$(du$, 1) = "2" Then
               du$ = du$ & "a"
            End If
         End If
      End If
      Debug.Print "du$=" & du$
     
      ' Check if it need an AND word
      If Res$ <> "" And Val(cdu$) Then
         If Val(cdu$) < 100 Or du$ = "00" Then
            Res$ = Res$ & "AND "
         End If
      End If
     
      If Val(cdu$) Then
         If Val(cdu$) < 100 Then
            If Casas = 1 And (du$ = "ONE" Or du$ = "01a") Then
            Else
               Res$ = Res$ & " n" & du$
            End If
         ElseIf Left$(cdu$, 1) = "1" And du$ <> "00" Then
            Res$ = Res$ & " HUNDRED AND" & du$
         ElseIf Val(cdu$) = 100 Then
            Res$ = Res$ & " n100"
         Else
            Res$ = Res$ & " n" & Left$(cdu$, 1) & "00" _
                        & IIf(PlayType = Female, "a", "") _
                        & IIf(du$ <> "00", "e", "")
            If du$ <> "00" Then
               Res$ = Res$ & " n" & du$
            End If
         End If
         
         If Val(cdu$) = 1 Then
            Res$ = Res$ & Choose(Casas, " thousand", " milion", " ne9", " ne12")
         Else
            Res$ = Res$ & Choose(Casas, " n1000", " ne6s", " ne9s", " ne12s")
         End If
     
      End If
                                         
      Casas = Casas - 1
   Next i
   
   If PlayType = Money Then
      If Val(Intg$) = 1 Then
         Res$ = Res$ & " Dolars"
      ElseIf CDbl(Number) = 0 Then
         Res$ = "n0 Reais"
      ElseIf Right$(Res$, 3) = "ne6" _
          Or Right$(Res$, 3) = "ne9" _
          Or Right$(Res$, 4) = "ne12" _
          Or Right$(Res$, 4) = "ne6s" _
          Or Right$(Res$, 4) = "ne9s" _
          Or Right$(Res$, 5) = "ne12s" Then
         Res$ = Res$ & " Dolars"
      ElseIf Val(Intg$) Then
         Res$ = Res$ & " Dolars"
      End If
   End If
   
   If Decm$ <> "" Then
      If Res$ <> "" Then
         Res$ = Res$ & "E " & PlayNumber(Mid$(Decm$, 2), Male, 0)
      Else
         Res$ = PlayNumber(Mid$(Decm$, 2), Male, 0)
      End If
     
      If PlayType = Money Then
         If Val(Mid$(Decm$, 2)) = 1 Then
            Res$ = Res$ & " cent"
         Else
            Res$ = Res$ & " cents"
         End If
      Else
         If Val(Mid$(Decm$, 2)) = 1 Then
            Res$ = Res$ & " f1" & String$(Decimals, "0")
         Else
            Res$ = Res$ & " f1" & String$(Decimals, "0") & "s"
         End If
      End If
   End If
   
   PlayNumber = LTrim$(Res$)
   
End Function


ASKER CERTIFIED SOLUTION
Avatar of MrE
MrE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LSILes

ASKER

Thanks much!  I'll have to make a few modifications, but it works like a charm!  Danka, danka, danka.  That'll save me a bunch of time.  And thanks to you other guys for the suggestions.  Much appreciated.

---Les

p.s.  Gave you a 15 point bonus, MrE.  Works great!
Avatar of LSILes

ASKER

Oops....   Guess I should grade instead of adding comments.  ;)  Thanks again!