Convert Currency to dollar words
page 1 of 1
Published: 13 Oct 2003
Unedited - Community Contributed
A converter like this is required for any Payroll or Accounts Payable check writing. Sometimes a modified version might be needed as a second description of important numbers in a transaction or such.
by Terry Voss
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 15091/ 17

57 to Fifty Seven

Convert Currency to dollar description words: (, w/explanation)
Author: Terry Voss

        Demo the Dollar Converter          Download the zip with 3 files: convert.aspx, convert.aspx.vb, dollar.vb          
        (extract 3 files into new web project, set convert.aspx as startup, F5)
A converter like this is required for any Payroll or Accounts Payable check writing. Sometimes a modified version might be needed as a second description of important numbers in a transaction or such. If you are going to print checks, remember that a large check amount like 1 million will take the entire length of the check, and you might want to consider having a 2 rows available for the description if large checks are the norm and the check allows it.
I've worked with a number of these converters and have had trouble understanding them enough to make it easy to modify or fix any found bug. This one is simple enough that I feel most people will be able to convert it easily to C#, or any language, and be able to modify the formatting to suite the client's check writing. I did not do this as a web service since it didn't make sense within one application and one server speedwise, and I wouldn't want to rely on a separate server on payroll day.
The dollar class has one public method, convert,  that relies on a private method ConvertHundreds which relies on private method ConvertTens. The input string is separated into dollars and cents. The cents are handled entirely within convert method. The dollars are separated after padding to 9 width with 0s to millions, thousands, and hundreds sections that all have 3 digits so that convertHundreds and convertTens will apply symmetrically to them.
Each method is simple enough that it is trivial to add billions, or remove hyphen in forty-five, or put and between each section. It would also be easy to remove reference to dollars and just refer to numbers also. A regular expression validator has been used to make sure the input string is valid enough for this conversion, though the conversion does complete the validation. The only framework elements required were array, replace, split, floor, substring, length, indexof, padleft, and padright. Therefore the only import is system.math for the floor method. Look at the code now and later I will discuss the 2 private methods further.
Imports System.Math
Public Class Dollar
  Dim oneWords As String = ",One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen"
  Dim ones() As String = oneWords.Split(",")
  Dim tenWords As String = ",Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety"
  Dim tens() As String = tenWords.Split(",")
Public Function Convert(ByVal input As String) As String
    input = input.Replace("$", "").Replace(",", "")
    If input.Length > 12 Then Return "Error in input value"
    Dim output, dollars, mills, thous, hunds, cents As String
    Dim mill, thou, hund, cent As Integer
    If input.IndexOf(".") > 0 Then
      dollars = input.Substring(0, input.IndexOf(".")).PadLeft(9, "0")
      cents = input.Substring(input.IndexOf(".") + 1).PadRight(2, "0")
      If cents = "00" Then cents = "0"
      dollars = input.PadLeft(9, "0") : cents = "0"
    End If
    mill = CType(dollars.Substring(0, 3), Integer) : mills = convertHundreds(mill)
    thou = CType(dollars.Substring(3, 3), Integer) : thous = convertHundreds(thou)
    hund = CType(dollars.Substring(6, 3), Integer) : hunds = convertHundreds(hund)
    cent = CType(cents, Integer) : cents = convertHundreds(cent)
    output = IIf(mills.Trim = "", "", mills + " Million ")
    output += IIf(thous.Trim = "", "", thous + " Thousand ")
    output += IIf(hunds.Trim = "", "", hunds)
    output = IIf(output.Length = 0, "Zero Dollars and ", output + " Dollars and ")
    output = IIf(output = "One Dollars and ", "One Dollar and ", output)
    output += IIf(cents = "", "Zero", cents) + " Cents"
    Return output
End Function
Private Function convertHundreds(ByVal input As Integer) As String
  Dim output As String
  If input <= 99 Then
    output = (convertTens(input))
    output = ones(Floor(input / 100))
    output += " Hundred "
    If input - Floor(input / 100) * 100 = 0 Then
      output += ""
      output += "" + convertTens(input - Floor(input / 100) * 100)
    End If
  End If
  Return output
End Function
Private Function convertTens(ByVal input As Integer) As String
  Dim output As String
  If input < 20 Then
    output = ones(input)
    input = 0
    output = tens(CType(Floor(input / 10), Integer))
    input -= Floor(input / 10) * 10
  End If
  output = output + IIf(ones(input).Trim = "", "", "-" + ones(input))
  Return output
End Function
End Class
Cents are easily handled by a few cases in the public convert method. The dollars sections are all handled the same except for the different suffixes: Millions, Thousands, and hundreds. The section of three digits is handed to the private method convertHundreds simply to see if there is a non-zero first digit. If there is hundreds involved, then that part is stripped out and a phrase is added to the output and the remainder if it exists is handed to convertTens private method. The covertTens private method works similarly. It decides whether the case is one of 20 or higher or the special ones case which is simply handled by supplying the index to the ones array.

I'm planning on using this code in a web accounting package in the near future, so feedback will be appreciated.
Below is the regular expression that was used to tailor the validation that goes on in the dollar class:
Here is a similar piece of code also in
Send mail to Computer Consulting with questions or comments about this web site.
Last modified: October 10, 2003



User Comments

Title: Converting Numbers to Words   
Name: Jonathan Wood
Date: 2010-12-29 1:45:21 AM
I just got through writing code and an article that accomplishes this in C#. If anyone's interested, you can see it at
Title: Great Answer   
Name: Gaurav Trivedi
Date: 2008-04-11 2:48:46 AM
This is such a wonderful function that i was searching from last few days...
thanx very much for such a nice function...
thanx again..

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-23 8:50:51 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search