Thursday, February 24, 2011

Conversion from Currency into English Words


Function Dollars(ByVal amount As Decimal, _Optional ByVal EmptyStringIfZero As Boolean = True) As String
Dim result As String = String.Empty
Select Case amount
Case 0
       result = If(EmptyStringIfZero, String.Empty, "Zero ")
Case 1 To 19
       result = Choose(amount, "One ", "Two ", "Three ", "Four ", _
"Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", _
"Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", _
"Seventeen ", "Eighteen ", "Nineteen ")
Case 20 To 99
       result = Choose(amount \ 10 - 1, "Twenty ", "Thirty ", _
"Fourty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", _
"Ninety ") & Dollars(amount Mod 10)
Case 100 To 999
         result = Dollars(amount \ 100) & "Hundred" & IIf _
(amount >= 200, "s ", " ") & Dollars(amount Mod 100)
Case 1000 To 999999
         result = Dollars(amount \ 1000) & "Thousand" & IIf _
(amount >= 2000, "s ", " ") & Dollars(amount Mod 1000)
Case 1000000 To 999999999
         result = Dollars(amount \ 1000000) & "Million" & IIf _
(amount >= 2000000, "s ", " ") & Dollars(amount Mod 1000000)
Case Is >= 1000000000
         result = Dollars(amount \ 1000000000) & "Billion" & _
IIf(amount >= 2000000000, "s", " ") & Dollars(amount Mod 1000000000)
End Select
Return result
End Function
 
Public Function Cents(ByVal amount As Decimal) As String
Dim result As String = amount.ToString
If result.Contains("."c) Then
      result = result.Substring(result.IndexOf("."c) + 1)
Else
      result = "00"
End If
Return " and " & result & "/100"
End Function

No comments: