Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) Converting Numbers to text (Read 2719 times)
wildwood
Full Member
***
Offline


No personal text

Posts: 156
Location: New York
Joined: Apr 2nd, 2004
Converting Numbers to text
Nov 4th, 2005 at 12:51am
Print Post Print Post  
I am using the program offered for printing checks. The problem I am having is that the text, of the dollar amounts,  does not match the monetary dollar amount (number value). 
As an example I would like $1405.89 converted to one thousand four hundred five and 89/100************
The dollar value is always written correctly, it's the cents value that is generally wrong.

I basically have two elenents Net, which has the numerical monetary value and Amount which has the converted monetary value in text form.

I am enclosing the programing for the Amount element for your review.

Appreciate any help,
Peter

/* Money value to text converter
   by Tom Marcellus 9/2003
   For amounts less than $1 million
   Converts an amount like $3025.55 to
   Three Thousand Twenty Five and 55/100
   Requires Money field named "Net"
   and Text field named "Words"
*/

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 = Net

If vNet = "0" Then
STOP

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

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 6 digits
// to the left of the decimal & 2 to the right

Dollars = @Left(vNet, @Instr(vNet, ".") -1 )
Dollars = @Right("000000" + Dollars, 6)
Cents = @Right(vNet, 2)
Cents = @Right("00" + 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"

// Add asterisks to pad out the text string

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

Amount = vWords
@Msg(vWords)

// Use words = toUpper(vWords) for all uppercase




  
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: Converting Numbers to text
Reply #1 - Nov 4th, 2005 at 3:37am
Print Post Print Post  
You say the "cents" is wrong, but can you give a specific example of what "wrong" is?

What do you want to see, and what are you actually seeing?

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
wildwood
Full Member
***
Offline


No personal text

Posts: 156
Location: New York
Joined: Apr 2nd, 2004
Re: Converting Numbers to text
Reply #2 - Nov 4th, 2005 at 4:37am
Print Post Print Post  
Bob,
By the "cents" error I mean that if the Net element = $1305.59 the resulting Amount (text) element would be something like One Thousand Three Hundred five and 23/100. Not the text it should be: One Thousand Three Hundred five and 59/100.

Peter
  
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Converting Numbers to text
Reply #3 - Nov 4th, 2005 at 12:21pm
Print Post Print Post  
Peter,

Your are probably running into the same issue I did. I had to create my own custom rounding function to solve it. If you subscribe to Inside Sesame, you can find the details of my solution in the following article.

Inside Sesame, June 2005, "Round Money & Numbers Predictably with a Custom Function", page 17.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
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 Numbers to text
Reply #4 - Nov 4th, 2005 at 3:05pm
Print Post Print Post  
Carl,

As far as I know the rounding issue will not make a decimal number jump by .36 as per his example numbers. Plus no where in the code is @RND called.



Peter,

Give the following code a try. Let me know if the numbers coming out of it are correct.

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

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 = Net

If vNet = "0" Then
STOP

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

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 6 digits
// to the left of the decimal & 2 to the right

Dollars = @Left(vNet, @Instr(vNet, ".") -1 )
Dollars = @Right("000000" + Dollars, 6)
Cents = @Right(vNet, @Len(vNet) - @Instr(vNet, "."))
Cents = @Left(Cents + "00", 2)
//Cents = @Right(vNet, 2)
//Cents = @Right("00" + 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"

// Add asterisks to pad out the text string

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

Writeln(Net)
Writeln(vWords) 



-Ray
  

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


No personal text

Posts: 156
Location: New York
Joined: Apr 2nd, 2004
Re: Converting Numbers to text
Reply #5 - Nov 4th, 2005 at 7:04pm
Print Post Print Post  
Ray,
Your code did the trick, almost.
In your code when I got to the Amount field a window opened up with the Net dollar amount and beneath it the converted text of that dollar amount. However it would not place it in the Amount element field. The other problem was that it did not round off the cents amount so for example 1230.7889 became One Thousand Two Hundred Thirty and 78/100 ****. Instead of 79/100******.
I deleted the Writeln(Net) and Writeln(vWords) codes and added Amount=Words. This put the written text into the Amount element but in some cases did not round off the cent amount as it did with your coding but in the sesame window.
The Net field is actually the net salary after the gross salary has various withholding taxes deducted. So, in some cases the net salary is rounded off to the nearest cents.

Peter
  
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 Numbers to text
Reply #6 - Nov 4th, 2005 at 7:19pm
Print Post Print Post  
Hello Peter,

The original problem was that the code you posted was grabbing the last two digits rather than the two that came right after the decimal point. This is why the decimal numbers were off in your original code. It was grabbing 38 from .5638, instead of 56.  I figured I would explain this for other people reading this thread.

As of right now, the code is not rounding at all. It just grabs the first two digits after the decimal and uses those.  That is what the following lines do.
Code
Select All
 Cents = @Right(vNet, @Len(vNet) - @Instr(vNet, "."))
Cents = @Left(Cents + "00", 2)  



To make it round you would want to change that section of the code.  You would want to do your rounding in between the two lines that I posted above.

-Ray
  

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


No personal text

Posts: 156
Location: New York
Joined: Apr 2nd, 2004
Re: Converting Numbers to text
Reply #7 - Nov 4th, 2005 at 7:50pm
Print Post Print Post  
Hi Ray,
I'll give you an example of what is happening.
If for example the gross salary is $679.00 the FICA tax is .0620 of the gross eqauling $42.10, the Medicare tax is .0145 of that gross equaling $9.85 the total deductions are $51.94. Subtracting this from the gross leaves a Net of $627.06. With your coding I get the text of: Six Hundred Twenty Seven and 05/100***********
I know its off by only a penny but I think employees would prefer it in their pocket.

Thanks much,
Peter
  
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 Numbers to text
Reply #8 - Nov 4th, 2005 at 8:20pm
Print Post Print Post  
Hello Peter,

The reason for that is because the code is grabbing the 05 right after the decimal places. The entire decimal is .0565  To make it round you need to add a line, or two, in between the following two lines.
Code
Select All
Cents = @Right(vNet, @Len(vNet) - @Instr(vNet, "."))
Cents = @Left(Cents + "00", 2) 



-Ray
  

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


No personal text

Posts: 156
Location: New York
Joined: Apr 2nd, 2004
Re: Converting Numbers to text
Reply #9 - Nov 4th, 2005 at 9:51pm
Print Post Print Post  
Ray,
You've been a great help and I know we are close to resoving the problem.
Carl answered earlier about an article in Inside Sesame, June 2005, "Round Money & Numbers Predictably with a Custom Function", page 17.
The code used to "cure" this penny problem is:

//##### Begin Custom Rounding Function #####
Function @cuRND(Value as double, Places as int) as
double
var vNeg as int //Used to handle Negatives properly
var vStr as string
if Value < 0
vNeg = -1
else
vNeg = 1
vStr = @Str(@Abs(Value))
if @Instr(vStr, “.”) > 0 //Protects whole numbers
when rounding to zero places
{
if @Mid(vStr, @Instr(vStr, “.”) + Places + 1,
1) >= 5
vStr = @Str( @TN(vStr) + (1 / @Exp(10,Places)) )
vStr = @Del(vStr, @Instr(vStr, “.”) + Places +
1, 10)
}
vStr = @Str(@TN(vStr) * vNeg)
Return @TN(vStr)
End Function
//###### End Custom Rounding Function #######

I am, as you have probably guessed by now, am pretty new to Sesame and havent the slightest clue how to or in what elements to place this programming.
Perhaps you could let me know what line or lines you were talking about in your earlier post, to place in between the two lines you displayed.

Peter
  
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Converting Numbers to text
Reply #10 - Nov 5th, 2005 at 2:22am
Print Post Print Post  
Quote:
Carl,

As far as I know the rounding issue will not make a decimal number jump by .36 as per his example numbers. Plus no where in the code is @RND called.

Ray,

I disagree. Wink

The problem with that older version of the Money-to-Words code that Peter posted is, it plucks the last two digits at the right of vNet (the numeric dollar value) by using "Cents = @Right(vNet, 2)". This will end up with the wrong numbers if the value has more than 2 digits after the decimal place, as in $1.8936. (Which I now see you mentioned in one of your subsequent posts.)

This is where the custom @cuRND function will force the value to have just 2 digits after the decimal place, and will round it in a predictable way, unlike the standard @RND, which sometimes does not round to what we would expect. Hence, Peter's quote below.

Quote:
The other problem was that it did not round off the cents amount so for example 1230.7889 became One Thousand Two Hundred Thirty and 78/100 ****. Instead of 79/100******.


I know that the newer Money-to-Text code will fix the value printed on the checks, but it still leaves the underlying value that Sesame stores on disk as a different value if that value had more than 2 digits after the decimal point. I prefer to force a money value to always ACTUALLY have just 2 digits after the decimal point, not simply display it that way.

Peter is describing exactly the same thing that I had run into, which was discussed in the following 2 threads. That is why and when I ended up creating the custom @cuRND function in the first place.

http://www.lantica.com/Forum2/cgi-bin/yabb/YaBB.pl?board=gen_disc;action=display...

http://www.lantica.com/Forum2/cgi-bin/yabb/YaBB.pl?board=gen_disc;action=display...
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Converting Numbers to text
Reply #11 - Nov 5th, 2005 at 2:51am
Print Post Print Post  
Quote:
Hi Ray,
I'll give you an example of what is happening.
If for example the gross salary is $679.00 the FICA tax is .0620 of the gross eqauling $42.10, the Medicare tax is .0145 of that gross equaling $9.85 the total deductions are $51.94. Subtracting this from the gross leaves a Net of $627.06. With your coding I get the text of: Six Hundred Twenty Seven and 05/100***********
I know its off by only a penny but I think employees would prefer it in their pocket.

Thanks much,
Peter


Peter,

This (the Medicare Tax of 0.0145) is EXACTLY the same issue that prompted me to create @cuRND. I too, was finding that sometimes the values were off by 1 cent, which threw things off at the end of the quarter.

You can check out the 2 links I put in my reply to Ray. They document the confusion around this issue, and it's eventual solution, which was summed up in the article I wrote.

@cuRND will predictably round up or down the way a human would - not the way the IEEE floating-point standard does. So, even though the Money-to-Text code can be fixed to work without rounding the value, I directed you to the article because I felt you would benefit by having the amount that was displayed in your money element actually match what was saved on the disk for the reasons mentioned in the article.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Converting Numbers to text
Reply #12 - Nov 5th, 2005 at 3:20am
Print Post Print Post  
Peter,

Regarding where to place the @cuRND function - you would place it in the Global Code event.

Then you need place this code in the On Element Exit event for each element you want to round. This will take care of rounding manually entered values.
Code
Select All
ThisElement = @cuRnd(ThisElement, 2)
 


To take care of values placed in an element by programming, you will want to use it to round your variables or calculations, as in the these examples.
Code
Select All
Income Tax = @cuRnd(vIncTax, 2)

Social Security Employee Tax = @cuRnd(Gross Pay * .062, 2)

Medicare Employee Tax = @cuRnd(Gross Pay * .0145, 2)

Employer Withholdings = @cuRnd(Income Tax + (Gross Pay * .124) + (Gross Pay * .029), 2)
 


  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
wildwood
Full Member
***
Offline


No personal text

Posts: 156
Location: New York
Joined: Apr 2nd, 2004
Re: Converting Numbers to text
Reply #13 - Nov 5th, 2005 at 5:18pm
Print Post Print Post  
Carl and Ray many thanks for your help. Problem resolved!!!!

Peter
  
Back to top
 
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Converting Numbers to text
Reply #14 - Sep 18th, 2006 at 4:43pm
Print Post Print Post  
Carl wrote:
Quote:
Regarding where to place the @cuRND function - you would place it in the Global Code event.

Would this work for all my databases if I placed it in the programming of the application?
(Application Property Manager -> Program Application -> Element: Application)
  

**
Captain Infinity
Back to top
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send Topic Send Topic Print Print