四舍五入问题不用delphi round函数

function RoundFloat(f: double; i: integer): double;
var
s: string;
ef: extended;
begin
s := '#.' + StringOfChar('0', i);
ef := StrToFloat(FloatToStr(f)); //防止浮点运算的误差
result := StrToFloat(FormatFloat(s, ef));
end;

 

====================================

function GetRound(iValue: double): Double;
var
iTemp: integer;
iMod: Integer;
begin
//先乘1000,用Trunc取整,除10取余,余数再取整,如果大于5,进位,小于5不进位。

iTemp := Trunc(iValue * 1000);
iMod := iTemp mod 10;

if Trunc(iMod) >= 5 then
Result := (Trunc(iValue * 100) + 1) / 100
else
Result := Trunc(iValue * 100) / 100;
end;

posted @ 2022-06-09 11:31  绿水青山777  阅读(53)  评论(0编辑  收藏  举报