Hi, Nic.

> Anyone had this problem (and a fix :) ):
>
> MS Access database, via the BDE (5.01). Invoice item table,
> has a currency field for the amount. Amounts range from a few dollars
> (and cents) to a few thousand. I need to sum them up. This works:
>
> select sum(amount) as total from invoiceLine
>
> calc's it just fine.
>
> BUT, I need to add lines, which are uncommitted, and provide a sum of
> that
> also. This DOES NOT work:
>
> total := 0;
> while not tblInvoice.eof do begin
>   total := total + tblInvoice.FieldByName("amount").AsCurrency;
> end;
>
> It has a habit of screwing with the post-decimal value, ie:
>
> 75.6100 becomes 75.6099
>
> accumulate that over a few hundred or thousand records and, well, its a
> BIG problem, especially in a financial-type app!!!
>
> Anyone know of a way to get accurate values out of the BDE?? I've had to
> create a temp table and doa cross-table sum, but I'd much rather do this
> in code.....
>
> HELP! :)

Hmm.. looks like you're running into the 80x87 IEEE rounding model, which
isn't helped by the fact the the Delphi supplied SysUtils.pas and Maths.pas
rounding routines are rather weak.

How have you declared Total? As a Extended, a Float, or a Currency?

Changing it to Currency if it isn't already may ensure Delphi generates
better 80x87 assembler code with less IEEE rounding artifacts. A faint hope
but worth a shot.

Alternatively, you should be rounding the final result to the correct number
of decimal places (in this case 2) which will give you the desired answer
(that's what we do).

Ideally, you will want a rounding routine which returns an Extended (unlike
Delphi's Round which returns either a Longint or an Int64) and preferably
does financial rounding rather than engineering rounding (i.e. Round(x)
= -Round(-x)) in case you are ever summing credits.

TTFN,
  Paul.


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to