Approach User Support homepage Approach User Support
Answers to Frequently Asked Questions about Lotus Approach
Examples LotusScripts
Example databases
Links to other Lotus Approach support services

[Return to contents]

Example LotusScript: ConvertNumberToText

Last updated: before December, 1998

'Written by Bruno Dumon, 1996 (Belgium)
'Copyright 1996 by Bruno Dumon
'
'Permission is granted to freely copy this script in electronic form,
'or to print for personal use. It may be use in any Approach database,
'but may not be distributed for profit either by itself or as part of
'a collection or database.  It may also be used in other programs
'using lotusscript, like WordPro, Notes or Freelance. 
'
'Disclaimer: This script is provided as is without any express or implied
'warranties. The author assumes no responsibility for errors or omissions,
'or for damages resulting from the use of the information contained herein.
'
'If you have any suggestions or problems, mail Bruno Dumon at
'dumona@unicall.be.  Also, if you make any improvements to it,
'please mail me a copy of it.
'
'How To Use It:
'=============
'Copy and paste the three following functions into the script editor:
'   - ConvertNumberToText
'   - ConvertTwoNumbers
'   - ConvertThreeNumbers
'Then, somewhere in your own code, call the ConvertNumberToText
'function by putting:
'    result$ = ConvertNumberToText(x)
'The script works for numbers ranging from 1 to 999,999,999.
Function ConvertNumberToText(getal)
        Dim e$(10)
        Dim t$(10)
        Dim h$(10)
        e$(1) = "one"
        e$(2) = "two"
        e$(3) = "three"
        e$(4) = "four"
        e$(5) = "five"
        e$(6) = "six"
        e$(7) = "seven"
        e$(8) = "eight"
        e$(9) = "nine"
        e$(10) = "ten"
        t$(1) = "eleven"
        t$(2) = "twelve"
        t$(3) = "thirteen"
        t$(4) = "fourteen"
        t$(5) = "fifteen"
        t$(6) = "sixteen"
        t$(7) = "seventeen"
        t$(8) = "eighteen"
        t$(9) = "nineteen"
        t$(10) = "twenty"
        h$(1)="ten"
        h$(2)="twenty"
        h$(3)="thirty"
        h$(4)="fourty"
        h$(5)="fifty"
        h$(6)="sixty"
        h$(7)="seventy"
        h$(8)="eighty"
        h$(9)="ninety"
        h$(10)="hundred"
        g$ = Format$(getal)  'Make a string of the number
        L = Len(g$)
        If L >=1 Then
                o1$ = e$(Val(Right$(g$,1)))
                If Len(g$) = 1 Then r$ = o1$:Goto finished
        End If
        If L >= 2 Then
                o2$ = ConvertTwoNumbers(g$,e$(),h$(),t$())
                If Len(g$) = 2 Then r$ = o2$:Goto finished
        End If
        If L >=3 Then
                o3$ = ConvertThreeNumbers(g$,e$(),h$(),t$())
                If Len(g$) = 3 Then r$ = o3$:Goto finished
        End If
        If L >= 4 Then
                v$ = Right$(g$,4)
                If Val(Left$(v$,1)) <> 1 Then
                        o4$ = e$(Val(Left$(v$,1))) + " thousand " + o3$
                Else
                        o4$ = " thousand " + o3$
                End If
                If Len(g$) = 4 Then r$ = o4$: Goto finished
        End If
        If L >= 5 Then
                v$ = Right$(g$,5)
                o5$ = ConvertTwoNumbers(Left$(v$,2),e$(),h$(),t$()) + " thousand " + o3$
                If Len(g$) = 5 Then r$ = o5$:Goto finished
        End If
        If L >=6 Then
                v$ = Right$(g$,6)
                o6$ = ConvertThreeNumbers(Left$(v$,3),e$(),h$(),t$()) + " thousand "  + " "+o3$
                If Len(g$) = 6 Then r$ = o6$:Goto finished
        End If
        If L >= 7       Then
                v$ = Right$(g$,7)
                o7$ = e$(Val(Left$(v$,1))) + " million " + ConvertThreeNumbers(Mid$(v$,2,3),e$(),h$(),t$()) + " thousand " + o3$
                If Len(g$) = 7 Then r$ = o7$: Goto finished
        End If
        If L >= 8       Then
                v$ = Right$(g$,8)
                o8$ = ConvertTwoNumbers(Left$(v$,2),e$(),h$(),t$()) + " million " + ConvertThreeNumbers(Mid$(v$,3,3),e$(),h$(),t$()) + " thousand " + o3$
                If Len(g$) = 8 Then r$ = o8$: Goto finished
        End If
        If L >= 9       Then
                v$ = Right$(g$,9)
                o9$ = ConvertThreeNumbers(Left$(v$,3),e$(),h$(),t$()) + " million " + ConvertThreeNumbers(Mid$(v$,4,3),e$(),h$(),t$()) + " thousand " + o3$
                If Len(g$) = 9 Then r$ = o9$: Goto finished
        End If finished:
        ConvertNumberToText = r$
End Function
Function ConvertThreeNumbers(what$,e$(),h$(),t$())
        v$ = Right$(what$,3)
        v2$ = ConvertTwoNumbers(Right$(what$,2),e$(),h$(),t$())
        If Left$(v$,1) = "0" Then ConvertThreeNumbers = ConvertTwoNumbers(v$,e$(),h$(),t$())    :Exit Function
        If Val(v$) = 0 Then ConvertThreeNumbers = "":Exit Function
        If Val(Left$(v$,1)) <> 1 Then
                If Val(v2$) <> 0 Then
                        ConvertThreeNumbers = e$(Val(Left$(v$,1))) + " hundred and " + v2$
                Else
                        ConvertThreeNumbers = e$(Val(Left$(v$,1))) + " hundred " + v2$
                End If
        Else
                If Val(v2$) <> 0 Then
                        ConvertThreeNumbers = " hundred and " + v2$
                Else
                        ConvertThreeNumbers = " hundred " + v2$
                End If
        End If
End Function
Function ConvertTwoNumbers(what$,e$(),h$(),t$())
        v$ = Right$(what$,2)
        If Val(v$) >=11 And Val(v$) <= 19 Then
                ConvertTwoNumbers = t$(Val(v$)-10)
        Else
                If Val(Right$(v$,1)) <> 0 Then
                        ConvertTwoNumbers = h$(Val(Left$(v$,1))) + "-" + e$(Val(Right$(v$,1)))
                Else
                        ConvertTwoNumbers = h$(Val(Left$(v$,1)))
                End If
        End If
End Function

[Return to contents]

© Copyright, JohnBrown, Trademarks, Disclaimer, Acknowledgements.