Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) MONEY VALUES ROUNDING (Read 3783 times)
PianoMan
Member
Members
*
Offline


No personal text

Posts: 44
Joined: Oct 13th, 2004
MONEY VALUES ROUNDING
Mar 23rd, 2005 at 9:46pm
Print Post Print Post  
I am having trouble with money values rounding. For example, sometimes when a tax amount is calculated, then the tax is added to the subtotal, the result will be off by one cent.

What is the best way to avoid having this happen?

I am using  variables as type "double".  I assume one needs to use the @TM() function, but I need advice on  exactly what to do to avoid rounding!!

Thanks

PianoMan
  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: MONEY VALUES ROUNDING
Reply #1 - Mar 23rd, 2005 at 9:59pm
Print Post Print Post  
Normally one avoids this by not rounding until the last possible operation. If you post your calculation code, we may be able to spot where the truncation is happening.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: MONEY VALUES ROUNDING
Reply #2 - Mar 23rd, 2005 at 10:39pm
Print Post Print Post  
That is, of course, due to Sesame's patented (well pending, anyways) "Debt Sensor". When properly configured, "SDebtBot" detects if the money value is owed to you, in which case it rounds up - or owed by you, in which case it rounds down. Wink
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
PianoMan
Member
Members
*
Offline


No personal text

Posts: 44
Joined: Oct 13th, 2004
Re: MONEY VALUES ROUNDING
Reply #3 - Mar 23rd, 2005 at 11:58pm
Print Post Print Post  
SBebtBot! Exactly what I need! LOL
Anyway, here is some code that once in a while rounds.

Thanks

PianoMan

var vAmount as Double
var vBalance as Double
var vNewBalance as Double

vAmount = Amount
If vAmount > 0
{
vBalance = @FormFieldValue("INVOICE", "Balance_Due", 0)
vNewBalance = @TM(vBalance) - @TM(vAmount)
FormFieldValue("INVOICE", "Balance_Due", 0, vNewBalance)
}
  
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: MONEY VALUES ROUNDING
Reply #4 - Mar 24th, 2005 at 3:48am
Print Post Print Post  
PianoMan,

I think your problem is at the point where Sesame performs the formatting for a money element, not in the SBasic programming.

I JUST went through this literally yesterday! (I have already reported it to Ray.)

You will find that if you enter $8.885 in a money element, Sesame will inform you that it is converting it to $8.88, where it should be $8.89. This does not happen in every example - only certain values cause this.

I corrected the problem by using @Rnd() like this:
Income Tax = @Rnd(vIncTax,2)

Where Income Tax is the Money LE, and vIncTax holds my final calculation for income tax.

Hope this helps.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
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: MONEY VALUES ROUNDING
Reply #5 - Mar 24th, 2005 at 4:57am
Print Post Print Post  
I seem to recall that some other programs (Access/VB/Visual?) sometimes rounded differently on Odd/Even numbers for the last digit 5.
The justification was that it was supposed to provide more accuracy with larger groups of numbers.
-----------------------------------------------------------
Another unexpected problem in other languages resulted from calculations  using floating-point numbers.
Example:
Int(3.555 * 100) results in 356., but
Int(4.555 * 100) results in 455 instead of 456.
-----------------------------------------------------

Does Sesame have any specific rules/exceptions like these mentioned from other programs? 
The only limitation I could see was the rounding range was limited from -15 to +15.

Will last digit 1-4 always round down?
Will last digit 5 always round up?
Will last digit 6-9 always round up?

  



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



Posts: 2530
Joined: Nov 22nd, 2002
Re: MONEY VALUES ROUNDING
Reply #6 - Mar 24th, 2005 at 3:17pm
Print Post Print Post  
This is not really a Sesame problem, and some might argue that it is not a problem at all. Sesame, like many other programs using the standard libraries provided by the operating system (Windows or Linux) uses the IEEE standard for floating point values (including money). That standard specifies rounding "rules". These rules include the inconvenient concept of alternating "breakpoints" for rounding on fives, wherein the number looks to the previous digit and alternately round up or down depending on the odd/even value of that digit.

The intent, one can assume, is to raise accumulated accuracy when rounding a large series of numbers, at the sacrifice of general predictability.

Microsoft (and Linux) both implement to the IEEE standard in the libraries almost all programs use to run on their operating systems. Strangely, and somewhat disconcertingly, MS do not themselves round that way if numbers are formatted using the operating system "locale" defaults. So to avoid this behavior in Sesame, simply set the OS defaults as needed, and in Sesame specify SYSTEM as the format.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: MONEY VALUES ROUNDING
Reply #7 - Mar 24th, 2005 at 4:24pm
Print Post Print Post  
Quote:
These rules include the inconvenient concept of alternating "breakpoints" for rounding on fives, wherein the number looks to the previous digit and alternately round up or down depending on the odd/even value of that digit.

Mark,

Thank you for this explanation! I always knew that floating point calculations did this sort of thing. But I don't rememeber anybody ever explaining exactly why, they would just say that floating point is more accurate and non-floating point (fixed-point?) calculations are more precise.

Now I understand what was meant by that statement, as you point out, it "...is to raise accumulated accuracy when rounding a large series of numbers...".
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
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: MONEY VALUES ROUNDING
Reply #8 - Mar 24th, 2005 at 4:26pm
Print Post Print Post  
Well Mark, you have confirmed my suspicions from my earlier message.  I agree, some discussion could be held re whether or not it is a problem, I am glad to avoid that.  But, some internal rules do exist.

And whatever the rules are, I think that we are most interested in the predictability so that we can respond accordingly.  With that said, I'm not sure that the questions have been answered.  What are the rules regarding predictability?  Is it only rounding of 5s that is a problem? If only floating-point calculations are affected, how/can we avoid floating-points?  Which are floating-point values, which are not?

You suggested that we "simply set the OS defaults as needed".  Could you also explain where those settings are located for rounding ?  I  have checked Regional Settings for number formats, but see no rounding rules options.
----------------------------------------------------------

Rounding Up Tip:
Sometimes, usually in Money calculations, it is desired to ALWAYS round up any fraction.  In that case, if the result is not an integer, just add "5" into the last decimal position and then "round"* the result.

Examples:
If value is not a complete integer:

Rounding to integer:
1.1 + .5 results in  2.0
1.5 + .5 results in 2.0
1.8 + .5 results in 2.0

Rounding to one place:
2.11 + .05 results in 2.20
2.14 + .05 results in 2.20
2.69 + .05 = 2.70

Rounding to 2 places:
3.414 + .005 results in 3.420
3.001 + .005 results in 3.010
3.688 + .005 results in 6.690

*Rounding predictability may be affected by OS rules as mentioned above.  And as Erika has mentioned earlier, you should generally do all your calculations first, and then just round the final result.
-----------------------------------------------------------
And I believe the famous SDebtBot uses this same approach but it subtracts .5  when owed by you, vs. adds .5 when due to you.
  



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: MONEY VALUES ROUNDING
Reply #9 - Mar 24th, 2005 at 4:52pm
Print Post Print Post  
Quote:
You suggested that we "simply set the OS defaults as needed".  Could you also explain where those settings are located for rounding ?  I  have checked Regional Settings for number formats, but see no rounding rules options.


Hello Bob,

There are no system settings for rounding. The OS Defaults Mark was referring to are on the first tab of the Regional and Language Options. If you set your System Number Format to have two decimal places, Then a number element in Sesame formatted to "SYSTEM" will display two decimal places, and numbers ending in a 5 or above will be rounded up.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
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: MONEY VALUES ROUNDING
Reply #10 - Mar 24th, 2005 at 4:59pm
Print Post Print Post  
Quote:
I corrected the problem by using @Rnd() like this:
Income Tax = @Rnd(vIncTax,2)


Hello Carl,

@Rnd() does not always round up numbers ending in 5 either. For example @Rnd(8.895, 2) will round to 8.89. If you format the number element to System, and you have the default number format for the united states, 8.895 will round to 8.90

-Ray
  

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



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: MONEY VALUES ROUNDING
Reply #11 - Mar 24th, 2005 at 5:03pm
Print Post Print Post  
Bob,

MyMoneyLE = @Rnd(vMyFinalMoneyValue, 2)

This will solve the rounding issue, plus you don't need to remember to check the OS systems settings on every computer you install your applicaton on, because it will already be in the application.

The only time this won't take care of it, is if someone types $8.885 from the keyboard. But, if that happens, Sesame will show a warning message indicating what it is changing it to.

Edit:
Forgot to mention that @Rnd appears to be following the rule of always rounding a 5 up.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
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: MONEY VALUES ROUNDING
Reply #12 - Mar 24th, 2005 at 5:45pm
Print Post Print Post  
Thanks Carl.....

But why does Ray's information and yours appear inconsistent?  ???

From Ray: Quote:
@Rnd() does not always round up numbers ending in 5 either
From you: Quote:
Forgot to mention that @Rnd appears to be following the rule of always rounding a 5 up.


Does it make a difference if rounding a calculation or a straight data entry value?

It sounds like the only rounding issue is based on "5"s.  I think I will sit back and watch you and Ray resolve this for us  Grin...... enjoy.
  



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



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: MONEY VALUES ROUNDING
Reply #13 - Mar 24th, 2005 at 6:07pm
Print Post Print Post  
WHOOPS!  Shocked

I didn't see Ray's last post. Judging by the timestamps, I was in the process of writing my post.

I just built a small app to test some various things... and Ray is correct.  Embarrassed

After I added @Rnd to one of my apps, I was getting the same exact values as the Q&A DB it was replacing, so I thought I had solved my problem. The problem values I found were $8.885 & $3.235, which were corrected with @Rnd, so I thought this was solved. But, it looks like @Rnd and element formatting are each following different rules for floating-point rounding.

Bottom line:
Mark & Ray have the correct solution. That is, "simply set the OS defaults as needed, and in Sesame specify SYSTEM as the format."

Sorry about the confusion.   Roll Eyes

########## EDIT: New bottom line solution ########## Smiley

Using the OS settings still allows the raw value to be saved to disk without rounding it to 2 decimal places.  This could present a problem if you were to export the data, or if you were to move the application to a computer that did not have the same OS settings for currency display format. Also, it's that raw non-rounded value that will be used by SBasic for other functions. For example, if you are using that value to build a money-to-words line for a check, it will use the raw data, which may not be the same as is displayed in the money element.

After a lot of frustration with the built-in functions not giving me what I wanted, I wrote my own user-defined function. So far, it has consistantly given me the results I wanted - which is to always round money up when the 3rd digit after the decimal point is a "5".

Code
Select All
//########## 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 ##########
 


@cuRnd(V, P)
Parameters: V as Double, P as Integer
Returns: Double

Rounds off the value of V to P decimal places. Unlike the regular @Rnd function, the value of P must be a positive number, meaning that it is only designed to round to whole numbers or to tenths, hundredths, thounsandths, etc. - not to the nearest tens, hundreds, etc.
« Last Edit: May 5th, 2005 at 1:54pm by Carl Underwood »  


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


No personal text

Posts: 44
Joined: Oct 13th, 2004
Re: MONEY VALUES ROUNDING
Reply #14 - Mar 24th, 2005 at 7:01pm
Print Post Print Post  
Thanks for the replies.

For my specific problem, I have been able to verify that by using @Rnd(vAmount, 2) whenever a field or variable might possibly have more than 2 decimal places returns the expected money values every time. However, looks like Ray's advice will eliminate other possible rounding "mistakes".

Does @ToMoney() work the same as @Rnd(MyMoney, 2)?

Thanks

PianoMan

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