|
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"
Else
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))
Else
output =
ones(Floor(input / 100))
output += " Hundred "
If input - Floor(input / 100) *
100 = 0 Then
output +=
""
Else
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
Else
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
|