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
The usage is also very simple:
Dim amount As Decimal = Decimal.Parse("123.45") Response.Write(Dollars(amount) & Cents(amount))
P.S. if you are using culture that use comma for decimal separator please modify the cents function respectively.
No comments:
Post a Comment