Delphi 的 Round() 四捨五入函數採奇入偶捨方式, Ex:
Round(1.5) => 2
Round(2.5) => 3
Round(3.5) => 4
解決之道: 自己寫四捨五入函數, 以下提供兩種:
方法一:
function MyRound(P: Double; Decimals: integer): Double;
var
factor: LongInt;
help: Double;
i: integer;
begin
factor := 1;
for i := 1 to decimals do
factor := factor * 10;
if P < 0 then
help := -0.5
else
help := 0.5;
Result := Int(P*factor+help) / factor;
if (Result > -0.00000001) and (Result < 0.00000001) then
Result := 0.00;
end;
方法二: 利用 FloatToStrF 及 StrToFloat 函數:
function MyRound(P: Double; Decimals: integer): Double;
var
s: string;
begin
s := FloatToStrF(P, ffFixed, 15, Decimals)
Result := StrToFloat(s);
end;
               (
geocities.com/huanlin_tsai)