Entisoft Tools icon -- a wrench Entisoft Tools Source Code Sample

The following function is a sample of the Microsoft Visual Basic source code that is included within the professional edition of the Entisoft Tools. Please note that the source code is NOT included with the standard edition. This function spells out a numeric amount in English words; it is part of the text processing class within the Entisoft Tools library.


Public Function SpellCurrency( _
      ByVal vvarDollars As Variant _
    , Optional ByVal vvarSpellTheCents As Variant _
    ) As Variant
    ' Accepts a number representing currency, and returns the value written in words.
    ' For example:
    '    SpellCurrency(34.23, False) = "Thirty-four and 23/100 Dollars"
    ' or
    '    SpellCurrency(34.23, True) = "Thirty-four Dollars and Twenty-three Cents"

    Const European = False

    If IsNull(vvarDollars) Then
        SpellCurrency = Null
        Exit Function
    ElseIf Not estV.FixUpVariantToCurrencyMaybeFn(vvarDollars) Then
        SpellCurrency = ""
        Exit Function
    'Note: Function only handles values between -1E36 and 1E36 exclusive.
    ElseIf vvarDollars <= -1E+36 Or vvarDollars >= 1E+36 Then
        SpellCurrency = "Overflow"
        Exit Function
    End If

    ' Assume that cents will NOT be spelled-out.
    estV.FixUpVariantToIntegerMaybeSub vvarSpellTheCents, False

    Static astrSuffixes(0 To 11) As String
    If astrSuffixes(1) <> "Thousand" Then
        If Not European Then
            astrSuffixes(0) = ""
            astrSuffixes(1) = " Thousand"
            astrSuffixes(2) = " Million"
            astrSuffixes(3) = " Billion"
            astrSuffixes(4) = " Trillion"
            astrSuffixes(5) = " Quadrillion"
            astrSuffixes(6) = " Quintillion"
            astrSuffixes(7) = " Sextillion"
            astrSuffixes(8) = " Septillion"
            astrSuffixes(9) = " Octillion"
            astrSuffixes(10) = " Nonillion"
            astrSuffixes(11) = " Decillion"
        Else
            astrSuffixes(0) = ""
            astrSuffixes(1) = " Thousand"
            astrSuffixes(2) = " Million"
            astrSuffixes(3) = " Milliard"
            astrSuffixes(4) = " Billion"
            astrSuffixes(5) = " Thousand Billion"
            astrSuffixes(6) = " Trillion"
            astrSuffixes(7) = " Thousand Trillion"
            astrSuffixes(8) = " Quadrillion"
            astrSuffixes(9) = " Thousand Quadrillion"
            astrSuffixes(10) = " Quintillion"
            astrSuffixes(11) = " Thousand Quintillion"
        End If
    End If

    Dim dblDollars As Double
    dblDollars = Fix(Abs(vvarDollars))
    Dim intCents As Integer
    intCents = (Abs(vvarDollars) - dblDollars) * 100

    Dim strResult As String

    If dblDollars = 0 Then
        If vvarSpellTheCents Then strResult = "Zero "
    ElseIf dblDollars >= 1100# And dblDollars < 2000# Then
        ' Handle numbers in this range as a special case--write-out something like "Twelve Hundred ...."
        If dblDollars Mod 100 <> 0 Then
            strResult = SpellThreeDigitInteger(dblDollars Mod 100) + " " + strResult
        End If
        strResult = SpellThreeDigitInteger(dblDollars \ 100) + " Hundred " + strResult
        dblDollars = dblDollars Mod 100
    ElseIf dblDollars > 0 Then
        ' Otherwise, examine every set of three digits, starting with the least significant.
        Dim intKSets As Integer
        For intKSets = 0 To (Fix(estM.LogNReal(dblDollars, 10)) \ 3)
            Dim dblCurValue As Double
            dblCurValue = dblDollars - Int(dblDollars / 1000#) * 1000#
            dblDollars = Int(dblDollars / 1000#)
            If dblCurValue <> 0 Then
                strResult = SpellThreeDigitInteger(dblCurValue) + astrSuffixes(intKSets) + " " + strResult
            End If
        Next intKSets
    End If

    ' Add "and xx/100 Cents", if appropriate.
    If Not vvarSpellTheCents And intCents <> 0 Then
        If Fix(vvarDollars) <> 0 Then strResult = strResult + "and "
        strResult = strResult + Format$(intCents, "0") + "/100 "
    End If
    
    If vvarSpellTheCents And Fix(Abs(vvarDollars)) = 1 Or Abs(vvarDollars) = 1 Then
        strResult = strResult + "Dollar"
    Else
        strResult = strResult + "Dollars"
    End If

    If vvarSpellTheCents And intCents <> 0 Then
        strResult = strResult + " and " + SpellThreeDigitInteger(intCents)
        If intCents = 1 Then
            strResult = strResult + " Cent"
        Else
            strResult = strResult + " Cents"
        End If
    End If

    ' Prefix string with the word "Negative", if appropriate.
    If vvarDollars < 0 Then strResult = "Negative " + strResult

    SpellCurrency = strResult
End Function

Return to the Entisoft Tools Page
Return to the Entisoft Home Page
Copyright © 1998 Entisoft. All Rights Reserved.