Normal Topic Re: Converting Dollar Amounts to Words (Read 750 times)
ST
Member
*
Offline



Posts: 9
Joined: Apr 5th, 2011
Re: Converting Dollar Amounts to Words
Apr 26th, 2011 at 2:56pm
Print Post Print Post  
I did some trials and found that the error only happens when the amount is over one hundred thousand and the word for dollar is always the same as word for thousand, if the thousand is seven, the dollar will return seven also. Any help to correct the programming is appreciated.
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Converting Dollar Amounts to Words
Reply #1 - Apr 26th, 2011 at 3:00pm
Print Post Print Post  
Hello,

Try the programming below should produce the correct results for you. It is looking like one of the If statements needed to be inside of another If statement and not right after it. After making this change I tried it with the number you gave and it appears to be correct.

-Ray

Code
Select All
var vAmount as String
var vWords as String
var Dollars as String
var Cents as String
var Chunk as String
var leftdigit as Int
var rightdigit as Int
var digits as Int
var BigOnes as array[9] of String
var SmallOnes as array[19] of String

Subroutine ParseChunk()

	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

vAmount = Amount
If (vAmount <> "0") And (@Int(vAmount) <= 999999) And (vAmount > 0) Then
{
	If @Instr(vAmount, ".") = 0 Then
	{
		vAmount = vAmount + ".0000"
	}
	// 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 6 digits
	// to the left of the decimal & 2 to the right
	Dollars = @Left(vAmount, @Instr(vAmount, ".") - 1)
	Dollars = @Right("000000" + Dollars, 6)
	Cents = @Right(vAmount, 4)
	Cents = @Left(Cents, 2)
	// Separate the dollars into chunks
	If @Int(Dollars) = 0 Then
	{
		vWords = "Zero"
	}
	Else
	{
		// First do the thousands
		Chunk = @Left(Dollars, 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
	// If Cents = 0 Then
	// Cents = "00"
	vWords = vWords + " and " + Cents + "/100"
	// Optionally add asterisks to pad out the string

	If @Len(vWords) < 65 Then
	{
		vWords = vWords + @Text(72 - @Len(vWords), "*")
	}
	Words = vWords
}
Else
{
	@MsgBox("Amount must be more than 0 and less than 1,000,000.", "","")
}
  

  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
ST
Member
*
Offline



Posts: 9
Joined: Apr 5th, 2011
Re: Converting Dollar Amounts to Words
Reply #2 - Apr 26th, 2011 at 3:18pm
Print Post Print Post  
Yes! problem solved, thanks a lot.
  
Back to top
 
IP Logged