//返回整数的四种情况
const
a = 1.8;
b = -1.8;
begin
{返回比值大的最小整数:}
ShowMessage(IntToStr(Ceil(1.8)) + ',' + IntToStr(Ceil(-1.8))); {返回:2,-1}
{返回比值小的最大整数:}
ShowMessage(IntToStr(Floor(1.8)) + ',' + IntToStr(Floor(-1.8))); {返回:1,-2}
{删除小数部分:}
ShowMessage(IntToStr(Trunc(1.8)) + ',' + IntToStr(Trunc(-1.8))); {返回:1,-1}
{Round}
ShowMessage(IntToStr(Round(1.8)) + ',' + IntToStr(Round(-1.8))); {返回:2,-2}
end;
{经我反复测试, 按性能排个序: Round、Trunc、Floor、Ceil; 应该多用 Round, 它的速度是 Trunc 的一倍以上}
//四舍五入函数: System.Math.SimpleRoundTo
uses System.Math;
procedure TForm1.FormCreate(Sender: TObject);
var
f, f1,f2: Double;
begin
f := 2.5555555;
f1 := SimpleRoundTo(f); //2.56
f2 := SimpleRoundTo(f, -3); //2.556
ShowMessageFmt('%g, %g, %g', [f, f1, f2]);
end;