converting numbers(currency) to strings in VB

Posted on 2004-11-03
Last Modified: 2010-04-17
I would like to know about a predifined function, or a sort of algorithm, that converts the numbers (Currency) to strings.
example when I calculate an amout of a bill if it rings $200.57 , it should say Two Hundred Dollars and Fifty Seven Cents.
Thanks you,
Question by:Chuckbuchan
    LVL 7

    Expert Comment

    Hi Chuckbuchan,

    See this link:

    LVL 48

    Accepted Solution

    Private Sub Command1_Click()

    'first set up two arrays to convert numbers to words
        Dim BigOnes(9) As String
        Dim SmallOnes(19) As String
    'and populate them
        BigOnes(1) = "Ten"
        BigOnes(2) = "Twenty"
        BigOnes(3) = "Thirty"
        BigOnes(4) = "Forty"
        BigOnes(5) = "Fifty"
        BigOnes(6) = "Sixty"
        BigOnes(7) = "Seventy"
        BigOnes(8) = "Eighty"
        BigOnes(9) = "Ninety"
        SmallOnes(1) = "One"
        SmallOnes(2) = "Two"
        SmallOnes(3) = "Three"
        SmallOnes(4) = "Four"
        SmallOnes(5) = "Five"
        SmallOnes(6) = "Six"
        SmallOnes(7) = "Seven"
        SmallOnes(8) = "Eight"
        SmallOnes(9) = "Nine"
        SmallOnes(10) = "Ten"
        SmallOnes(11) = "Eleven"
        SmallOnes(12) = "Twelve"
        SmallOnes(13) = "Thirteen"
        SmallOnes(14) = "Fourteen"
        SmallOnes(15) = "Fifteen"
        SmallOnes(16) = "Sixteen"
        SmallOnes(17) = "Seventeen"
        SmallOnes(18) = "Eighteen"
        SmallOnes(19) = "Nineteen"
    'format the incoming number to guarantee six digits
    'to the left of the decimal point and two to the right
    'and then separate the dollars from the cents
        Text1.Text = Format(Text1.Text, "000000.00")
        Dollars = Left(Text1.Text, 6)
        Cents = Right(Text1.Text, 2)
        Words = ""

    'check to make sure incoming number is not too large
        If Dollars > 999999 Then
            Text2.Text = "Dollar amount is too large"
            Exit Sub
        End If
    'separate the dollars into chunks
        If Dollars = 0 Then
            Words = "Zero"
    'first do the thousands
            Chunk = Left(Dollars, 3)
            If Chunk > 0 Then
                GoSub ParseChunk
                Words = Words & " Thousand"
            End If
    'do the rest of the dollars
            Chunk = Right(Dollars, 3)
            If Chunk > 0 Then
                GoSub ParseChunk
            End If
        End If
    'concatenate the cents and display
        If Cents = 0 Then Cents = "No"
        Words = Words & " and " & Cents & "/100"
        Text2.Text = Words
        Exit Sub
        digits = Mid(Chunk, 1, 1)
        If digits > 0 Then
            Words = Words & " " & SmallOnes(digits) & " Hundred"
        End If
        digits = Mid(Chunk, 2, 2)
        If digits > 19 Then
            leftdigit = Mid(Chunk, 2, 1)
            rightdigit = Mid(Chunk, 3, 1)
            Words = Words & " " & BigOnes(leftdigit)
            If rightdigit > 0 Then
                Words = Words & " " & SmallOnes(rightdigit)
            End If
            If digits > 0 Then
                Words = Words & " " & SmallOnes(digits)
            End If
        End If

    End Sub

    Original project from this tip by  
    by Burt Abreu. An updated version of this project is
    available below (VB6 Sample 2) and was created
    by Brian Duckworth.

     Download VB6 Sample 1

    Updated Version
    'Re-written as a class module
    'by Brian Duckworth

    I've place the conversion code into a class component so that it can be dropped into any project. Although I used VB6 to develop it, you should have no problem using it with VB5.

    To test it, you'll need a form and a class module. The form's code comments contain the required controls to place on the form.

    You may notice that this code allows for a rather large range of valid values. I accomplished this without loss of precision by using the rather obscure data type of Decimal (refer to the CDec function in the help for a description of this data type).

    'Form:  FMoney
    'The form contains four controls:
    '   Text1        - Holds numeric value to convert
    '   RichTextBox1 - Holds results of conversion
    '   Command1     - Triggers the conversion
    '   Command2     - Exits the program
    'Add the above four controls and cut-and-paste the following as the form's
    'procedural code.

    Option Explicit

    Private Sub Command1_Click()
      Dim Money As CMoney
      Set Money = New CMoney
      RichTextBox1.Text = Money.MonetaryToWords(Text1.Text)
      Set Money = Nothing
    End Sub

    Private Sub Command2_Click()
      Unload Me
    End Sub

    'Class Module:  CMoney
    'Cut-and-paste the following as the class' procedural code.

    Option Explicit
    'Persist the "words" across all calls to this class
    Private m_19AndUnder(0 To 19) As String
    Private m_Tens(2 To 9)        As String
    Private m_Hundred             As String
    Private m_Groups(1 To 10)     As String
    Private m_Dollar              As String
    Private m_Dollars             As String
    Private m_NoCents             As String
    Private m_Cent                As String
    Private m_Cents               As String
    Private m_Hyphen              As String
    Private m_And                 As String
    Private m_InvalidAmount       As String

    Private Sub Class_Initialize()
      'Initialize all the "words"
      m_19AndUnder(0) = "Zero"
      m_19AndUnder(1) = "One"
      m_19AndUnder(2) = "Two"
      m_19AndUnder(3) = "Three"
      m_19AndUnder(4) = "Four"
      m_19AndUnder(5) = "Five"
      m_19AndUnder(6) = "Six"
      m_19AndUnder(7) = "Seven"
      m_19AndUnder(8) = "Eight"
      m_19AndUnder(9) = "Nine"
      m_19AndUnder(10) = "Ten"
      m_19AndUnder(11) = "Eleven"
      m_19AndUnder(12) = "Twelve"
      m_19AndUnder(13) = "Thirteen"
      m_19AndUnder(14) = "Fourteen"
      m_19AndUnder(15) = "Fifteen"
      m_19AndUnder(16) = "Sixteen"
      m_19AndUnder(17) = "Seventeen"
      m_19AndUnder(18) = "Eighteen"
      m_19AndUnder(19) = "Nineteen"
      m_Tens(2) = "Twenty"
      m_Tens(3) = "Thirty"
      m_Tens(4) = "Forty"
      m_Tens(5) = "Fifty"
      m_Tens(6) = "Sixty"
      m_Tens(7) = "Seventy"
      m_Tens(8) = "Eighty"
      m_Tens(9) = "Ninety"
      m_Hundred = "Hundred"
      m_Groups(1) = ""
      m_Groups(2) = "Thousand"
      m_Groups(3) = "Million"
      m_Groups(4) = "Billion"
      m_Groups(5) = "Trillion"
      m_Groups(6) = "Quadrillion"
      m_Groups(7) = "Quintillion"
      m_Groups(8) = "Sextillion"
      m_Groups(9) = "Septillion"
      m_Groups(10) = "Octillion"
      m_Dollar = " Dollar"
      m_Dollars = " Dollars"
      m_NoCents = "No Cents"
      'm_Cent & m_Cents could both be changed to "/100"
      m_Cent = " Cent"
      m_Cents = " Cents"
      'Used for #s like: 23 = "Twenty-Three"
      m_Hyphen = "-"
      'Used between dollars & cents: "One Dollar and 12 Cents"
      m_And = " and "
      m_InvalidAmount = "Invalid Dollar Amount."
    End Sub

    Public Function MonetaryToWords(Value As Variant) As String
      Dim decValue    As Variant
      Dim sValue      As String
      Dim iDecimal    As Integer
      Dim sCents      As String
      Dim sDollars    As String
      On Error GoTo ER
      'Convert input into a Decimal value (up to 28 digits)
      decValue = CDec(Value)
      If decValue <0 Then GoTo ER 'Convert the Decimal value back into a string. This eliminates ' any format characters such as "$" or ",". sValue="CStr(decValue)" 'Find the decimal point & extract the dollars from the cents iDecimal="InStr(1," sValue, ".") If iDecimal="0" Then sDollars="sValue" sCents="00" Else 'Extract decimal value sCents="Mid$(sValue," iDecimal + 1) If Len(sCents)> 2 Then GoTo ER
        'Extract dollars
        sDollars = Left$(sValue, iDecimal - 1)
        'Fill-out decimal places to two digits
        sCents = Left$(sCents & "00", 2)
      End If
      'At this point,
      '  sDollars = the whole dollar value (0.. approx 79 Octillion)
      '  sCents   = the cents (00..99)
      Debug.Assert Len(sCents) = 2
      Debug.Assert Len(sDollars) > 0
      Debug.Assert Len(sDollars) <31 Select Case sCents Case "00" sCents="m_NoCents" Case "01" sCents="sCents" & m_Cent Case Else sCents="sCents" & m_Cents End Select MonetaryToWords="DollarsToWords(sDollars)" & m_And & sCents Exit Function ER: MonetaryToWords="m_InvalidAmount" End Function Private Function DollarsToWords(sDollars As String) As String Dim sWords As String Dim decValue As Variant Dim sRemaining As String Dim s3Digits As String Dim iGroup As Integer Dim i100s As Integer Dim i10s As Integer Dim i1s As Integer Dim i99OrLess As Integer Dim sWork As String 'We had better be passing a valid number Debug.Assert IsNumeric(sDollars) 'Check for special cases. This also serves to validate the value decValue="CDec(sDollars)" Select Case decValue Case 0 DollarsToWords="m_19AndUnder(decValue)" & m_Dollars Exit Function Case 1 DollarsToWords="m_19AndUnder(decValue)" & m_Dollar Exit Function End Select 'There should be no insignificant zeroes, "punctuation" or decimals Debug.Assert sDollars="CStr(decValue)" iGroup="0" sRemaining="sDollars" sWords 'Extract each group of three digits, convert to words and prefix to result While Len(sRemaining)> 0
        iGroup = iGroup + 1
        'Extract next group of three digits
        If Len(sRemaining) > 3 Then
          s3Digits = Right$(sRemaining, 3)
          sRemaining = Left$(sRemaining, Len(sRemaining) - 3)
          'Fill-out group to three digits
          s3Digits = Right$("00" & sRemaining, 3)
          sRemaining = ""
        End If
        Debug.Assert Len(s3Digits) = 3
        If s3Digits <> "000" Then
          i100s = CInt(Left$(s3Digits, 1))
          i10s = CInt(Mid$(s3Digits, 2, 1))
          i1s = CInt(Right$(s3Digits, 1))
          i99OrLess = (i10s * 10) + i1s
          sWork = " " & m_Groups(iGroup)
          Select Case True
            'Do we have 20..99?
            Case i10s > 1
              Debug.Assert i10s <= 9 If i1s> 0 Then
                Debug.Assert i1s <= 9 sWork="m_Tens(i10s)" & m_Hyphen & m_19AndUnder(i1s) & sWork Else sWork="m_Tens(i10s)" & sWork End If 'Do we have 01..19? Case i99OrLess> 0
              Debug.Assert i99OrLess <= 99 sWork="m_19AndUnder(i99OrLess)" & sWork Case Else 'If we're here, it's because there are no tens or ones Debug.Assert i99OrLess="0" Debug.Assert i10s="0" Debug.Assert i1s="0" Debug.Assert Right$(s3Digits, 2)="00" 'If there's no tens or ones, there better be hundreds Debug.Assert i100s> 0
          End Select
          If i100s > 0 Then
            Debug.Assert i100s <= 9 sWork="m_19AndUnder(i100s)" & " " & m_Hundred & " " & sWork End If Debug.Assert Len(Trim$(sWork))> 0
          sWords = sWork & " " & sWords
        End If
      DollarsToWords = Trim$(sWords) & m_Dollars
    End Function

    LVL 55

    Expert Comment

    by:Jaime Olivares
    LVL 2

    Expert Comment

    The answers above might be good.I think those are not sophisticated at all...let me tell you my idea.
    The easiest way is to read the number from left, every 3 main digits (e.g.  123 million 456 thousand 456) till you reach the decimal. Then you print " Dollars " and read the 2 numbers after the decimal...
    To be well organized, I'd used 4 or 5 methods. One of them receives a single digit to read, method2 receives 2 digits to read and obviously they return a string. You would call the methods recursively.
    So you put this in method 1 with a 'select case' which is easier than 'if' statements.
    and this in method 2:
    select case

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Suggested Solutions

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 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

    12 Experts available now in Live!

    Get 1:1 Help Now