[fpc-devel] Round function

Andrea Mauri andrea.mauri at unimib.it
Thu Oct 12 15:06:07 CEST 2006


Using round function I implemented the RoundTo function as in Delphi:

type
  TRoundToRange = -37..37;

function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
var
  LFactor: Double;
begin
  LFactor := IntPower(10, ADigit);
  Result := Round(AValue / LFactor) * LFactor;
end;

In Delphi RoundTo has a known bug 
(http://qc.borland.com/wc/qcmain.aspx?d=8070).
And in freepascal (Windows) I noticed the same behavior.
In order to respect the banker's rounding we must obtain:
RoundTo(1.235, -2) ->1.24
RoundTo(1.245, -2) -> 1.24

But in reality:
RoundTo(1.235, -2) ->1.24
RoundTo(1.245, -2) -> 1.25

I didn't test it on Linux yet.
It is a known bug?
a.



More information about the fpc-devel mailing list