Subject | Re: [IBO] Rounding problems |
---|---|
Author | Geoff Worboys |
Post date | 2001-08-08T05:06:05Z |
> I did what you said and multiply first.<...>
> If I debug the result is indeed 633.0991735537Hmmm, what I posted earlier was from my understanding of the online
> 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 ?
help. I just did what I should have to begin with, actually run a
small experiment in code (without any IBO). This is what I get when I
declare each of the working variables as currency...
var
a, b, c, x: currency;
begin
a := 766.05;
b := 121;
c := 100;
// 1. divide first results
x := a / b; // (766.05 / 121) = 6.331
x := x * c; // (6.331 * 100) = 633.1
// 2. multiple first
x := a * c; // (766.05 * 100) = 76605
x := x / b; // (76605 / 121) = 633.0992
// 3. all at once, divide first
x := (a / b) * c; // (766.05 / 121) * 100 = 633.0992
// 4. all at once, multiply first
x := (a * c) / b; // (766.05 * 100) / 121 = 633.0992
end;
Results 1 and 2 clearly indicate that rounding does indeed occur on
currency values (at 4 decimal places). Results 3 and 4 seem to imply
that Delphi currency values are probably using double or extended for
intermediate results - and not currency/int64 - this is the only way I
can explain getting the same answer for both calculations. This would
explain the results of the first two calculations as well.
> Do I have to round myself ? if so what is the best wayI think I said earlier that I always use doubles and then provide my
> to do it ?
own explicit rounding. This allows me to control and understand every
step of a calculation. As you can see above, Delphi sometimes says
one thing and does another. The problem with my approach is that it
can be significantly more work - in addition my approach requires UDFs
since it is generally at the server where I ensure the stored value is
rounded appropriately.
I think someone else has already posted a rounding example.
> Also something strange, if I tab into theMore information please. Are you using editmasks? What exactly are
> 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 ?
you seeing.
Geoff Worboys
Telesis Computing