[AccessD] Rounding Error - A2K

Gustav Brock Gustav at cactus.dk
Fri Dec 11 11:25:38 CST 2009


Hi Reuben

The result of this:

  CurAmount * CCur(100 * SngPercentage) / 100

is a double. That's why I converted it to currency:

  CCur(CurAmount * CCur(100 * SngPercentage) / 100)

but, of course, that will round to four decimals.
If you really need six decimals for the result, you have three options:

1. Leave it as a double. For later calculations, multiply by 100, calculate, divide by 100.

2. Multiply the result by 100 by leaving out the dividing by 100:

  CurAmount * CCur(100 * SngPercentage)

For later calculations, divide the result by 100.

3. Convert to decimal. This is a subtype of variant and is quite useful for interim calculations meaning results you need in your code without storing them to a table.
Newer version of Access features the decimal data type for tables, but it is known to be buggy in a dangerous and destructive way, so don't.

/gustav


>>> accessd at gfconsultants.com 11-12-2009 18:01 >>>
My question is...
What data type is the result of your equation?

I understant multiplying by 100 before converting to Currency in order to
maintain all the decimals.
However, when you divide by 100 what's the result?  If it stayed currency
wouldn't you lose two of the decimals?

Reuben Cummings
GFC, LLC
812.523.1017


> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com 
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of 
> Gustav Brock
> Sent: Friday, December 11, 2009 10:59 AM
> To: accessd at databaseadvisors.com 
> Subject: Re: [AccessD] Rounding Error - A2K
> 
> 
> Hi Reuben
> 
> What a relief! Thanks for the feedback!
> 
> /gustav
> 
> 
> >>> accessd at gfconsultants.com 11-12-2009 16:50 >>>
> Nope.  Just did it again and you're right.  Yours works fine.
> I must have misplaced something when I was trying it Wednesday night.
> Sorry.
> 
> Thanks, Gustav.
> 
> Reuben Cummings
> GFC, LLC
> 812.523.1017
> 
> 
> > -----Original Message-----
> > From: accessd-bounces at databaseadvisors.com 
> > [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of 
> > Gustav Brock
> > Sent: Thursday, December 10, 2009 8:45 AM
> > To: accessd at databaseadvisors.com 
> > Subject: Re: [AccessD] Rounding Error - A2K
> > 
> > 
> > Hi Reuben
> > 
> > I did some tests with no errors and returning the correct 
> > data type. Are you sure about this?
> > 
> > /gustav
> > 
> > 
> > >>> accessd at gfconsultants.com 10-12-2009 12:57 >>>
> > Thanks.  That appears to work.  However, I think your 
> > paranthesis are off.
> > For anyone wanting this for future use I had to change it to...
> > 
> > >   CurResult = CCur((CurAmount * CCur(100 * SngPercentage))/ 100)
> > 
> > Only added two paranthesis (to completely inclose the 
> > multiplication part),
> > but it makes sure the math gets done before it gets divided by 100.
> > 
> > Thanks for the help.
> > 
> > Reuben Cummings
> > GFC, LLC
> > 812.523.1017
> > 
> > 
> > > -----Original Message-----
> > > From: accessd-bounces at databaseadvisors.com 
> > > [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of 
> > > Gustav Brock
> > > Sent: Thursday, December 10, 2009 4:18 AM
> > > To: accessd at databaseadvisors.com 
> > > Subject: Re: [AccessD] Rounding Error - A2K
> > > 
> > > 
> > > Hi Reuben
> > > 
> > > There is your explanation: cur * sng returns a double.
> > > 
> > > What you could do is to keep storing the percentage as a 
> > > single. When using it for calculation, do:
> > > 
> > >   CurResult = CCur(CurAmount * CCur(100 * SngPercentage) / 100)
> > > 
> > > This will calculate using your six decimals and (with CCur) 
> > > round the result to four decimals.
> > > 
> > > /gustav






More information about the AccessD mailing list