四舍五入

//四舍五入加强版,可指定到小数后几位
function RoundPlus(Value: Extended;Length: Integer): Extended;

  procedure Set8087CW(NewCW: Word);
  asm
    MOV     Default8087CW,AX
    FNCLEX
    FLDCW   Default8087CW
  end;

const
  RoundUpCW         = $1B32;
var
  OldCW             : Word;
  s: string;
begin
  if Length = 0 then
    s := '0'
  else
    s := '0.' + Repl('0', Length);

  OldCW := Default8087CW;
  try
    Set8087CW(RoundUpCW);
    Result := StrToFloat(FormatFloat(s, VarToFloat(Value)));
  finally
    Set8087CW(OldCW);
  end;
end;

 

function RoundEx (const Value: Real): integer;
    var
      x: Real;
    begin
      x := Value - Trunc(Value);
      if x >= 0.5 then
        Result := Trunc(Value) + 1
      else Result := Trunc(Value);
    end;    

 

 

 
posted @ 2007-10-14 21:43  delphi中间件  阅读(265)  评论(0编辑  收藏  举报