I do a lot of technical programming, and "banker's" rounding does not do it for me.
My Delphi "MyUtils.pas" unit contains this function:
function RoundCorrect(R: Real): LongInt;
begin
Result:= Trunc(R); // extract the integer part
if Frac(R) >= 0.5 then // if fractional part >= 0.5 then...
Result:= Result + 1; // ...add 1
end;
and this works correctly with negative numbers too?
You are right, negative numbers need a bit of a correction:
function RoundCorrect(R: Real): LongInt;
begin
Result:= Trunc(R); // extract the integer part
if Abs(Frac(R)) >= 0.5 then // if fractional part >= 0.5 then...
Result := Result + Trunc(2.0 * Frac(R)); // add +1 or -1, depending on R's sign
end;
PS I wrote this for Delphi5, which to my knowledge did not have 'SetRoundMode' as an option.