Convert a Number to Words/Text

Posted on 2006-04-26
Last Modified: 2012-06-27
Hi all,

I am developing a receipting system and I want to take the amount paid; let say

$157.57 and convert that into to words being:

One Hundred Fifty Seven Dollars and Fifity Seven Cents.....

Is there any way I accomplish this in VB or VBA...

Thanks for you eager response...

Question by:menkiz
    LVL 16

    Accepted Solution

    Hi menkiz,

    Here's one:

    LVL 14

    Expert Comment

    Also take a look at following samples done in VB6

    Hope this helps

    Author Comment

    Exactly what I needed......

    Expert Comment

    Here's a version I wrote up specificaly for Currency, supporting large numbers

    Option Explicit
    ' Converts a String Dollar and Cents value like $123.45
    ' into a worded string value like One Hundred twenty-three dollars and Fourty-Five cents

    ' Function: CurrencyToWords
    ' Parameter: Amount To convert, a string representation of a currency amount (Dollars)
    ' Returns: A Enlish Text Word representation of the Currency value
    Function CurrencyToWords(AmountToConvert)
        Dim TempString
        Dim AmountSplit
        Dim Dollars
        Dim Cents
        AmountSplit = Split(AmountToConvert, ".")
        If Left(AmountSplit(0), 1) = "$" Then AmountSplit(0) = Right(AmountSplit(0), Len(AmountSplit(0)) - 1) 'Remove the Dollar Sign
        Dollars = NumberToWords(AmountSplit(0))
        If (UBound(AmountSplit) - LBound(AmountSplit)) >= 1 Then
                Cents = NumberToWords(AmountSplit(1))
                Cents = ""
            End If
        If (Len(Dollars) > 0) Then TempString = Dollars & " Dollars"
        If (Len(Cents) > 0) Then
            If (Len(TempString) > 0) Then TempString = TempString & " and "
            TempString = TempString & Cents & " Cents"
            End If
        CurrencyToWords = TempString
        End Function

    ' Function NumberToWords
    ' Converts a Whole number into a word string representation
    Function NumberToWords(NumbersToConvert)
        Dim TempString
        Dim TempNumbers
        Dim OnesPlace
        Dim TensPlace
        Dim HundresPlace
        Dim Numbers
        Dim Tens
        Dim Powers
        Dim PowersLoop
        Dim TempThousandsNum
        Dim TempThousandsWords

        Numbers = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
        Tens = Array("", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
        Powers = Array("", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion")

        TempNumbers = StrReverse(NumbersToConvert) ' Reverse the string so the numbers increase in String order
        For PowersLoop = 1 To (Len(TempNumbers)) Step 3 'Breaks the string up into Thousands
            TempThousandsNum = Mid(TempNumbers, PowersLoop, 3)
            OnesPlace = CInt(Mid(TempThousandsNum, 1, 1))
            TensPlace = 0
            HundresPlace = 0
            If (Len(TempThousandsNum) >= 2) Then
                TensPlace = CInt(Mid(TempThousandsNum, 2, 1))
                End If
            If (Len(TempThousandsNum) >= 3) Then
                HundresPlace = CInt(Mid(TempThousandsNum, 3, 1))
                End If
            TempThousandsWords = Numbers(OnesPlace)
            If (TensPlace > 1) Then
                TempThousandsWords = Tens(TensPlace) & "-" & TempThousandsWords
                TempThousandsWords = Numbers((TensPlace * 10) + OnesPlace)
                End If
            If (HundresPlace >= 1) Then TempThousandsWords = Numbers(HundresPlace) & " Hundred and " & TempThousandsWords
            TempThousandsWords = Trim(TempThousandsWords)
            If PowersLoop \ 3 > 0 Then TempThousandsWords = TempThousandsWords & " " & Powers(PowersLoop \ 3) & ","
            If (Len(TempThousandsWords) > 0) Then TempString = TempThousandsWords & " " & TempString
        NumberToWords = Trim(TempString)

    End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now