Conversion from Currency into English Words

Currency into Words
Currency into Words

I often need to convert the amounts into words when i create reports, checks and stuff. I found that the following recursive functions do the job perfectly. Most of the examples you can find out there are too long and confusing. This one seems to be shortest and str8 forward. Please check it out:

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.

One thought on “Conversion from Currency into English Words

  1. In C# the following code is done
    =================
    static void Main(string[] args)
    {
    Decimal amount = Decimal.Parse(“1293977028.45″);
    Console.Write(Dollars(Math.Floor(amount)) + Cents(amount));
    Console.ReadKey();
    }

    private static string Dollars(Decimal amount, Boolean EmptyStringIfZero = true)
    {
    string result = String.Empty;
    if (amount == 0)
    {
    result = EmptyStringIfZero ? String.Empty : “Zero “;
    }
    else if (amount >= 1 && amount = 20 && amount = 100 && amount = 200 ? “s ” : ” “) + Dollars(amount % 100);

    }
    else if (amount >= 1000 && amount = 2000 ? “s ” : ” “) + Dollars(amount % 1000);
    }
    else if (amount >= 1000000 && amount = 2000000 ? “s ” : ” “) + Dollars(amount % 1000000);
    }
    else if (amount >= 1000000000)
    {
    result = Dollars(Math.Floor(amount / 1000000000)) + “Billion” + (amount >= 2000000000 ? “s” : ” “) + Dollars(amount % 1000000000);
    }

    return result;
    }
    private static string Choose(decimal amount,Hashtable htblTemp)
    {
    return (string)htblTemp[int.Parse(amount.ToString())];
    }

    public static string Cents(Decimal amount)
    {
    string result = amount.ToString();

    if (result.Contains(“.”))
    {
    result = result.Substring(result.IndexOf(“.”) + 1);
    }
    else
    {
    result = “00”;
    }
    return ” and ” + result + “/100″;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>