Discussion:
RoundTo Problem with Delphi
(too old to reply)
Phlox
2003-07-30 11:16:31 UTC
Permalink
Hi,

is Delphi able to round in the following way:

1.235 -> 1.24
1.245 -> 1.25

The RoundTo function is retuning eiter way 1.24 !!!!

Thanks
John Leavey
2003-07-30 12:43:34 UTC
Permalink
Post by Phlox
Hi,
1.235 -> 1.24
1.245 -> 1.25
The RoundTo function is retuning eiter way 1.24 !!!!
RoundTo() calls Round() which uses "banker's rounding" - rounds towards nearest even
number.

For the rounding you want, replace RoundTo() with this :-


function MyRoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
var
LFactor: Double;
begin
LFactor := IntPower(10, ADigit);
Result := Trunc( ( AValue / LFactor ) + 0.5) * LFactor;
end;


John Leavey
Yahia El-Qasem
2003-07-30 16:00:54 UTC
Permalink
Use SimpleRoundTo from the Delphi RTL - it uses the rounding algorithm you
want...

Yahia
Achim Berg
2003-07-30 20:22:14 UTC
Permalink
Use SimpleRoundTo

Achim
Post by Phlox
Hi,
1.235 -> 1.24
1.245 -> 1.25
The RoundTo function is retuning eiter way 1.24 !!!!
Thanks
Martin Stoeckli
2003-07-31 07:37:20 UTC
Permalink
Hello

SimpleRoundTo would be the function to use, but unfortunately it has
problems with negative numbers.

example:
-8.4 will be rounded to -7

The problem is already logged.

Best regards:
Martin
Post by Yahia El-Qasem
Use SimpleRoundTo
Achim
Post by Phlox
Hi,
1.235 -> 1.24
1.245 -> 1.25
The RoundTo function is retuning eiter way 1.24 !!!!
Thanks
Continue reading on narkive:
Loading...