The one problem was in ParseChunk() the second time and third time it was called. I'll step through it line by line so that you can see what was happening. Let's pretend we are parsing the second chunk of 217.
digits = @Left(Chunk, 1)Grabs the first character of chunk so digits will be a 2
If digits > 0 Then Digits, which is a 2, is greater than 0 so the If statement will run it's conditional
vWords = vWords + " " + SmallOnes[digits] + " Hundred"Adds "2 Hundred" to the text already in vWords
digits = @Mid(Chunk, 2, 2)Grabs the next 2 characters of chunk so digits is now 17
If digits > 19 Then Digits, which is 17, is less than 19 so the code in this If statement will not run
{
leftdigit = @Mid(Chunk, 2, 1)
rightdigit = @Mid(Chunk, 3, 1)
vWords = vWords + " " + BigOnes[leftdigit]
} Those lines of code do not run
If rightdigit > 0 Then Here is where the problem is. rightdigit has not been set to a value since the last time through so it is still set to a 4
Since four is greater than 0 the code in this If statement is going to run
vWords = vWords + " " + SmallOnes[rightdigit] This line runs and adds a four to the text already strung together
Else If ((digits <= 19) and (digits > 0)) Then
{
vWords = vWords + " " + SmallOnes[digits]
}This code does not run as the If statement was true so the Else does not get a chance to run
The second problem had to do with how the code was grabbing the digits after the decimal place. A simple @Right will not work if the value after the decimal place is just a 7, as in 70 cents. For example $234.70 would be "234.7" and grabbing the rightmost two characters would return ".7"
Try the code below and let me know if it works for you.
var vNet as String
var vWords as String
var Dollars as String
var Cents as String
var Chunk as String
var digits as Int
var leftdigit as Int
var rightdigit as Int
var BigOnes as array[9] of String
var SmallOnes as array[19] of String
Var PeriodPos as Int
Subroutine ParseChunk() //########
Rightdigit = 0
Leftdigit = 0
digits = @Left(Chunk, 1)
If digits > 0 Then
{
vWords = vWords + " " + SmallOnes[digits] + " Hundred"
}
digits = @Mid(Chunk, 2, 2)
If digits > 19 Then
{
leftdigit = @Mid(Chunk, 2, 1)
rightdigit = @Mid(Chunk, 3, 1)
vWords = vWords + " " + BigOnes[leftdigit]
}
If rightdigit > 0 Then
{
vWords = vWords + " " + SmallOnes[rightdigit]
}
Else If ((digits <= 19) and (digits > 0)) Then
{
vWords = vWords + " " + SmallOnes[digits]
}
End Subroutine //##################
vNet = LoanAmount
If vNet <> "0" Then
{
If @Instr(vNet, ".") = 0 Then
{
vNet = vNet + ".00"
}
// Fill the two arrays.
BigOnes[1] = "Ten"
BigOnes[2] = "Twenty"
BigOnes[3] = "Thirty"
BigOnes[4] = "Forty"
BigOnes[5] = "Fifty"
BigOnes[6] = "Sixty"
BigOnes[7] = "Seventy"
BigOnes[8] = "Eighty"
BigOnes[9] = "Ninety"
SmallOnes[1] = "One"
SmallOnes[2] = "Two"
SmallOnes[3] = "Three"
SmallOnes[4] = "Four"
SmallOnes[5] = "Five"
SmallOnes[6] = "Six"
SmallOnes[7] = "Seven"
SmallOnes[8] = "Eight"
SmallOnes[9] = "Nine"
SmallOnes[10] = "Ten"
SmallOnes[11] = "Eleven"
SmallOnes[12] = "Twelve"
SmallOnes[13] = "Thirteen"
SmallOnes[14] = "Fourteen"
SmallOnes[15] = "Fifteen"
SmallOnes[16] = "Sixteen"
SmallOnes[17] = "Seventeen"
SmallOnes[18] = "Eighteen"
SmallOnes[19] = "Nineteen"
// Separate the dollars from the cents
// Format incoming number to ensure 9 digits
// to the left of the decimal & 2 to the right
PeriodPos = @Instr(vNet, ".")
Dollars = @Left(vNet, PeriodPos -1 )
Dollars = @Right("000000000" + Dollars, 9)
Cents = @ToNumber(vNet) - @ToNumber(Dollars)
Cents = @Round(@ToNumber(Cents), 2)
Cents = @Right(Cents, @Len(Cents) - @InStr(Cents, "."))
Cents = @Left(Cents + "00", 2)
// Separate the dollars into chunks
If @Int(Dollars) = 0 Then
{
vWords = "Zero"
}
Else
{
// First do the millions
Chunk = @Left(Dollars, 3)
If @Int(Chunk) > 0 Then
{
ParseChunk()
vWords = vWords + " Million"
}
// Then do the thousands
Chunk = @Mid(Dollars, 4, 3)
If @Int(Chunk) > 0 Then
{
ParseChunk()
vWords = vWords + " Thousand"
}
// Do the rest of the dollars
Chunk = @Right(Dollars, 3)
If @Int(Chunk) > 0 Then
{
ParseChunk()
}
}
// Concatenate the cents and display
vWords = vWords + " and " + Cents + "/100"
WriteAmount = vWords
@Msg(vWords)
}
-Ray