举例:
var
  i: Integer;
  d: Real;
begin
  i := Trunc(1234.5678);      {截取整数}
  ShowMessage(IntToStr(i));   {1234}
  i := Trunc(-1234.5678);
  ShowMessage(IntToStr(i));   {-1234}

  i := Round(1234.5678);      {...}
  ShowMessage(IntToStr(i));   {1235}
  i := Round(-1234.5678);
  ShowMessage(IntToStr(i));   {-1235}

  {Int 和 Trunc 都是向 0 方向舍入, 貌似结果一直, 但它们返回的类型不一样}
  {Trunc 返回的是 Int64; Int 返回的是 Extended 类型}
  d := Int(1234.5678);
  ShowMessage(FloatToStr(d)); {1234}
  d := Int(-1234.5678);
  ShowMessage(FloatToStr(d)); {-1234}
end;


//四舍五入函数: 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;


System 单元下的公用函数目录

posted on 2008-03-30 23:09  万一  阅读(3141)  评论(2编辑  收藏  举报