Subject | Re: [IBO] Rounding problems |
---|---|
Author | Leeway |
Post date | 2001-08-07T13:46:24Z |
Geoff,
I did what you said and multiply first.
QTIB_Art.FieldByName('EP_EXCL').AsCurrency :=
((QTIB_Art.FieldByName('EP_INCL').AsCurrency * 100) /
(QTIB_Art.FieldByName('VAT').AsCurrency + 100));
If I debug the result is indeed 633.0991735537
but the IBEdit shows still 633.09 so the roundig to two decimals
did not occur even if I use AsCurrency.
I figured that the Currency was doing the rounding ?
Do I have to round myself ? if so what is the best way to do it ?
Also something strange, if I tab into the IBEdit the correct
contents of the field are display'd if I use the mouse a complete
other amount is display'd any suggestions on that ?
The only event I use on the IBEdit is the OnExit
Lee
I did what you said and multiply first.
QTIB_Art.FieldByName('EP_EXCL').AsCurrency :=
((QTIB_Art.FieldByName('EP_INCL').AsCurrency * 100) /
(QTIB_Art.FieldByName('VAT').AsCurrency + 100));
If I debug the result is indeed 633.0991735537
but the IBEdit shows still 633.09 so the roundig to two decimals
did not occur even if I use AsCurrency.
I figured that the Currency was doing the rounding ?
Do I have to round myself ? if so what is the best way to do it ?
Also something strange, if I tab into the IBEdit the correct
contents of the field are display'd if I use the mouse a complete
other amount is display'd any suggestions on that ?
The only event I use on the IBEdit is the OnExit
Lee
--- In IBObjects@y..., "Geoff Worboys" <geoff@t...> wrote:
> > //to calculate the amount exclusive VAT :
> > // amount-including-vat divided by 100+vat% times 100
> > // (766.05 / (100 + 21.00)) * 100
> > // (766.05 / 121) * 100 = 633.0991 the fields are
> > // numeric(18,2) so it rounds on two decimals and should
give
> > // 633.10 but it gives 633.09 what am I doing wrong ?
>
> Multiply first, divide last.
>
> From the Borland online help about currency...
>
> - - - -
> Currency is a fixed-point data type that minimizes rounding errors
in
> monetary calculations. It is stored as a scaled 64-bit integer with
> the four least-significant digits implicitly representing decimal
> places. When mixed with other real types in assignments and
> expressions, Currency values are automatically divided or multiplied
> by 10000.
> - - - -
>
> Note that it is based on an integer. That is:
> (5 div 2) = 2
> NOT 3 (which would be the result if it was rounded).
>
>
> So by doing the divide first you end up with:
>
> (766.05 / (100 + 21.00)) * 100
> => (6.3309 * 100) // 6.3309917355 was truncated to 4 places
> => 633.0900
> => 633.09 // after rounding to 2 places
>
>
> If you multiply out first you get...
>
> (766.05 / (100 + 21.00)) * 100
> => (76605 / 121)
> => (633.0991) // 633.0991735537 was truncated to 4 places
> => 633.10 // after rounding to 2 places
>
>
>
> You alternative to this is to perform the calculates with doubles
and
> then convert back to numerics (currency or whatever). This can
often
> reduce the confusion when dealing with division, percentages etc.
>
>
> HTH
>
> Geoff Worboys
> Telesis Computing