CodeXchange Saturday, December 21, 2024
Home
What's it?
Download
Register
My Snippets
Add Snippet
Search
Faq
Contact Us
Sponsors

Website hosted by


Code Snippet Preview

Review the code snippet before inserting it on your project.

Snippet Metadata
Summary: Display Currency as text (Dollars & Cents)
Language: VB.NET
Author: Mark Conway
Author snippets RSS:
Culture: en-US
Bytes: 3392
Visual Studio 2005 Snippet:

Snippet Stats
Downloads: 10
Overall rating : 3
Average rating : Snippet rating

Snippet Preview
Code:
 Public Function CurrencyText(ByVal CurrencyValue As Double) As String
        Dim Cents As Integer
        Dim Dollars As String
        Dim Units(9) As String
        Dim Teens(9) As String
        Dim Tens(9) As String
        Teens(0) = "Ten" : Tens(0) = ""
        Units(1) = "One" : Teens(1) = "Eleven" : Tens(1) = "Ten"
        Units(2) = "Two" : Teens(2) = "Twelve" : Tens(2) = "Twenty"
        Units(3) = "Three" : Teens(3) = "Thirteen" : Tens(3) = "Thirty"
        Units(4) = "Four" : Teens(4) = "Fourteen" : Tens(4) = "Forty"
        Units(5) = "Five" : Teens(5) = "Fifteen" : Tens(5) = "Fifty"
        Units(6) = "Six" : Teens(6) = "Sixteen" : Tens(6) = "Sixty"
        Units(7) = "Seven" : Teens(7) = "Seventeen" : Tens(7) = "Seventy"
        Units(8) = "Eight" : Teens(8) = "Eighteen" : Tens(8) = "Eighty"
        Units(9) = "Nine" : Teens(9) = "Nineteen" : Tens(9) = "Ninety"
        Cents = (CurrencyValue - Int(CurrencyValue)) * 100
        Dollars = Format(Int(CurrencyValue), "000000000")
        Select Case Cents
            Case 0
                CurrencyText = "and 00/100 Dollars"
            Case Else
                CurrencyText = "and " & Format(Cents, " 00") & "/100 Dollars"
        End Select
        Select Case Mid(Dollars, Len(Dollars) - 1, 1) 'tens digit
            Case "1" 'last two digits are teens
                CurrencyText = Teens(Val(Right(Dollars, 1))) & " &" & CurrencyText
            Case Else
                CurrencyText = Tens(Val(Mid(Dollars, Len(Dollars) - 1, 1))) & " " & _
      Units(Val(Right(Dollars, 1))) & " &" & CurrencyText
        End Select
        If Mid(Dollars, Len(Dollars) - 2, 1) <> "0" Then 'hundreds digit
            CurrencyText = Units(Val(Mid(Dollars, Len(Dollars) - 2, 1))) & " Hundred " & CurrencyText
        End If
        Dollars = Left(Dollars, Len(Dollars) - 3) 'trim off hundreds,tens, & units
        If Right(Dollars, 3) <> "000" Then 'no thousands
            Select Case Mid(Dollars, Len(Dollars) - 1, 1) 'ten thousand digit
                Case "1" 'last two digits are teens
                    CurrencyText = Teens(Val(Right(Dollars, 1))) & " Thousand " & CurrencyText
                Case Else
                    CurrencyText = Tens(Val(Mid(Dollars, Len(Dollars) - 1, 1))) & " " & _
       Units(Val(Right(Dollars, 1))) & " Thousand " & CurrencyText
            End Select
            If Mid(Dollars, Len(Dollars) - 2, 1) <> "0" Then 'thousands digit
                CurrencyText = Units(Val(Mid(Dollars, Len(Dollars) - 2, 1))) & " Hundred " & CurrencyText
            End If
        End If
        Dollars = Left(Dollars, Len(Dollars) - 3) 'trim off thousands...leave millions
        If Right(Dollars, 3) <> "000" Then 'no millions
            Select Case Mid(Dollars, Len(Dollars) - 1, 1) 'ten million digit
                Case "1" 'last two digits are teens
                    CurrencyText = Teens(Val(Right(Dollars, 1))) & " Million " & CurrencyText
                Case Else
                    CurrencyText = Tens(Val(Mid(Dollars, Len(Dollars) - 1, 1))) & " " & _
       Units(Val(Right(Dollars, 1))) & " Million " & CurrencyText
            End Select
            If Mid(Dollars, Len(Dollars) - 2, 1) <> "0" Then 'millions digit
                CurrencyText = Units(Val(Mid(Dollars, Len(Dollars) - 2, 1))) & " Hundred " & CurrencyText
            End If
        End If
    End Function

Snippet Comments
Comments:
No comments for this snippet

Other snippets that may interest you..
Related Snippets
TDT - GridPage - Page_Init (C#)
CustomValitaor (C#)
(C#)
Get the file extension from a file path or file name (VB.NET)
grab record from db and subtracts it from variable (VB.NET)



Copyright ©2009-2024 CodeXchange. Server version 1.0.3720.32855 Client Version 0.9.0.0

With from Barcelona

Most Helpful members
These are the members who have received the most points for their helpful samples
Zepho Zep
Robert Wagner
Galen Taylor

All time 'Hall of fame'
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
INI File Access (VB.NET)
Read XML from string into DataSet (C#)
Create Manifest File for your Application (VB.NET)
Round function to avoid banker's rounding (VB.NET)
Get Short and Long Path Names (VB.NET)
Sending Mail through authenticated SMTP server (C#)
One Way Hash for strings (C#)
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
How do I load an image from a URI address? (VB.NET)
Use our easy to use Visual Studio.NET addin client and start sharing code snippets with the CodeXchange community!
Refreshed: Saturday, December 21, 2024