Normal Topic Modified Dollar to Text and Cents is weird (Read 1459 times)
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Modified Dollar to Text and Cents is weird
Oct 4th, 2006 at 6:00pm
Print Post Print Post  
I tried to modify Tom Marcellus' Money to Text Converter to allow amounts over $1 million.  It works correctly most of the time.  I am not a programmer and just learn by following existing code and reading the Sesame Programming Guide to see what the different code does.  If I enter most any amount in the hundreds of millions the cents will change.  For example if I enter 134217719.04, the cents changes to .99 instead of .04.  I have added a writeln after the cents and can see the error but do not know how to track it down any further.  Also, sometimes the cents will change to a decimal and a number. for example if I enter 50053.10  the cents changes to .1.

I have highlighted the changes that I made to the code.

Thank you for your help.

[code]/* Money value to text converter
   by Tom Marcellus 9/2003 – [highlight]Modified by GKM
   to accetpt amounts larger than $1 Million.[/highlight]
   Converts an amount like $3025.55 to
   Three Thousand Twenty Five and 55/100
   Requires Money field named "[highlight]LoanAmount[/highlight]"
   and Text field named "[highlight]WriteAmount[/highlight]"
*/

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

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 //##################

vNet = LoanAmount

If vNet = "0" Then
STOP

[highlight]//[/highlight]If @Int(vNet) > 999999 Then {
[highlight]//[/highlight]@Msgbox("","Dollar amount too large.","")
[highlight]//[/highlight]STOP
[highlight]//[/highlight]}

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 [highlight]9[/highlight] digits 
// to the left of the decimal & 2 to the right 

Dollars = @Left(vNet, @Instr(vNet, ".") -1 )
Dollars = @Right("[highlight]000[/highlight]000000" + Dollars, [highlight]9[/highlight])
Cents = @Right(vNet, 2)
Cents = @Right("00" + Cents, 2)


// Separate the dollars into chunks 

If @Int(Dollars) = 0 Then
vWords = "Zero" 

Else {

[highlight]// First do the millions 

Chunk = @Left(Dollars, 3) 

If @Int(Chunk) > 0 Then {
ParseChunk()
vWords = vWords + " Million" 
}
[/highlight]

// Then do the thousands 

Chunk = @[highlight]Mid[/highlight](Dollars,[highlight] 4,[/highlight] 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" 

// Add asterisks to pad out the text string

[highlight]//[/highlight]If @Len(vWords) < 60 Then
[highlight]//[/highlight]vWords = vWords + @Text(60 - @Len(vWords), "*")

WriteAmount = vWords
@Msg(vWords)

// Use words = toUpper(vWords) for all uppercase
[/code]
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Modified Dollar to Text and Cents is weird
Reply #1 - Oct 4th, 2006 at 8:42pm
Print Post Print Post  
CapitalG,

Try changing this line:
vNet = LoanAmount

... To this:
vNet = @Decimals(LoanAmount, 2)

Let me know if that fixes your problems.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Modified Dollar to Text and Cents is weird
Reply #2 - Oct 4th, 2006 at 9:19pm
Print Post Print Post  
That did help on the 134217719.04.   But now if I try 134217719.07  it returns 134204704.06

Is there somewhere in the code that I need to "clear out" the variables?

I'm lost.


Thanks
  
Back to top
 
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Modified Dollar to Text and Cents is weird
Reply #3 - Oct 4th, 2006 at 9:51pm
Print Post Print Post  
Just to narrow this down I created a new application with just two elements - LoanAmount and WriteAmount.   LoanAmount is currency and WriteAmount is Multiline text.   I get the same problem in this new application.  I added a bunch of writeln's and it appears that the variable rightdigit in the ParseChunk Subroutine is not returning the correct amount when the range falls between 1 and 19.   

Example - on the 134217719.04  the 134 part processes correctly but the 217 and the 719 do not.  Both of these "chunks" have the last two digits between 1 and 19.

  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Modified Dollar to Text and Cents is weird
Reply #4 - Oct 4th, 2006 at 11:09pm
Print Post Print Post  
This sounds vaguely familar.  Do a seach here for the Rounding function that was created by Carl Underwood.  It is possible that his function may solve this for you.

Check here: http://www.lantica.com/Forum3/cgi-bin/yabb2/YaBB.pl?num=1141468866/2#2
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
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: Modified Dollar to Text and Cents is weird
Reply #5 - Oct 5th, 2006 at 2:35pm
Print Post Print Post  
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.
Code
Select All
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
  

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



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Modified Dollar to Text and Cents is weird
Reply #6 - Oct 5th, 2006 at 2:52pm
Print Post Print Post  
As always - GREAT support!   Thank you so much. Smiley
  
Back to top
 
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: Modified Dollar to Text and Cents is weird
Reply #7 - Oct 5th, 2006 at 2:55pm
Print Post Print Post  
Oh yeah.  It worked perfectly. (Of course we had no doubts)

Thanks again.
  
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: Modified Dollar to Text and Cents is weird
Reply #8 - Oct 5th, 2006 at 3:14pm
Print Post Print Post  
CapitalG wrote on Oct 5th, 2006 at 2:52pm:
As always - GREAT support!   Thank you so much. Smiley



You are welcome.

-Ray
  

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