自带的round 好像跟我们平常说 四舍不入不太一样 比如round(10.5)=10
上网找了一个 但识别不出来 10.45=5这样的功能。

解决方案 »

  1.   

    function RoundClassic(R: Real): Int64;
    begin
    Result:= Trunc(R);
    if Frac(R) >= 0.5 then
        Result:= Result + 1;
    end;
      

  2.   

    10.45 -> 10
    10.50 -> 11
    8.49 -> 8
    9.96 -> 10
    12.58 -> 13
    试了一下,应该没问题
      

  3.   

    uses Math;function MyRound(Value: Extended; RoundMode: TFPURoundingMode = rmUp): Int64;
    var
      RM: TFPURoundingMode;
    begin
      RM := GetRoundMode;
      try
        SetRoundMode(RoundMode);
        Result := Round(Value);
      finally
        SetRoundMode(RM);
      end;
    end;例:myRound(10.45)->10.5
      

  4.   

    wzwcn  这个方法可行! 
     
      

  5.   

    round()是四舍五入;
    trunc()取整数;
    ceil()有小数位,就向整数位进一;
    format()取成统一格式的字符串.
      

  6.   

    //四舍五入保留小数点后len位数
    function Tsys_njqlrb_f.RoundClassic(R: Real;len:integer): real;
    var
      i,j:integer;
      str:string;
    begin
      if Trunc(R)=R then
      begin
        Result:= Trunc(R);
        exit;
      end;
      Result:= Trunc(R);
      str:=(copy(floattostr(R),length(inttostr(Trunc(R)))+2,len));
      str:='0.'+str;
      Result:=Result+strtofloat(str);
      j:=1;
      for i:=1 to len do  j:=j*10;
      if length(copy(floattostr(R),length(inttostr(Trunc(R)))+2+len,1))<>0 then
      begin
        if strtoint(copy(floattostr(R),length(inttostr(Trunc(R)))+2+len,1)) >= 5 then
          Result:= Result + 1/j;
      end;
    end; 
    代码是转摘的,希望能帮到楼主
      

  7.   

    function DoRound(Value: Extended): Int64; 
      procedure Set8087CW(NewCW: Word); 
      asm
        MOV Default8087CW,AX
        FNCLEX
        FLDCW Default8087CW
      end;
    const
      RoundUpCW = $1B32;
    var
      OldCW : Word;
    begin
      OldCW := Default8087CW; 
      try 
        Set8087CW(RoundUpCW); 
        Result := Round(Value); 
      finally 
        Set8087CW(OldCW); 
      end;
    end;
      

  8.   

    function RoundEx(Value: Extended; RoundMode: TFPURoundingMode = rmUp): Int64;
    var
      RM: TFPURoundingMode;
    begin
      RM := GetRoundMode;
      try
        SetRoundMode(RoundMode);
        Result := Round(Value);
      finally
        SetRoundMode(RM);
      end;
    end;
    举例:
    i := RoundEx(11.5)    结果: i=12
    i := RoundEx(10.5)    结果: i=11
      

  9.   

    这里的round是四舍六入五成双,也是比较合理的处理小数位的方法,如果楼主要常规的四舍五入的话,就是在第二位为5,而后面没有时,判断一下就好了,其他的都是一样的
      

  10.   


    function DealPrecision(number:real;precision:integer;dealtype:integer):real;
    var
      tmpNumber:real;
      tmpPrecision:integer;
    begin
      if Abs(number)<0.0001 then
      begin
        Result:=0;
        Exit;
      end;
      if precision>=0 then
      begin
        Result:=DealPrecisionZ(number,precision,dealtype);
        exit;
      end
      else
      begin
        tmpPrecision:=abs(precision);
        tmpNumber:=number/power(10,tmpPrecision);
        tmpNumber:=DealPrecisionZ(tmpNumber,1,dealtype);
        tmpNumber:=tmpNumber*power(10,tmpPrecision);
        Result:=tmpNumber;
      end;
    end;function DealPrecisionZ(number:real;precision:integer;dealtype:integer):real;
    var
      s:string;
      i:integer;
      tmp:string;
      strNumber:string;
      intLength,intIntCount,intPointPos,intFloatCount:integer;
      intZFFlag:Integer;
    begin
      if Abs(number)<0.0001 then
      begin
        Result:=0;
        Exit;
      end;
      if number<0 then
      begin
        intZFFlag:=-1;
      end
      else
      begin
        intZFFlag:=1;
      end;
      number:=intZFFlag*number;
      strNumber:=floattostr(number);
      if copy(strNumber,1,15)='0.0999999999999' then
      begin
        strNumber:='0.1';
      end;  if pos('.',strNumber)>0 then
      begin
        if pos('0000000000',strNumber)>pos('.',strNumber) then
        begin
          strNumber:=copy(strNumber,1,(pos('0000000000',strNumber)-1));
        end;
      end;  intPointPos:=Pos('.',strNumber);
      if intPointPos=0 then
      begin
        number:=intZFFlag*number;
        result:=number;
        exit;
      end;
      intLength:=length(strnumber);
      intIntCount:=intPointPos-1;
      intFloatCount:=intLength-intPointPos;
      s:='0.';
      for i:=1 to (precision+1) do
      begin
        s:=s+'0';
      end;
      case dealtype of
        0 :
        begin
          number:=StrToFloat(copy(FloatToStr(number),1,intPointPos+(Length(s)-2)));
          tmp:=FormatFloat(s,(number+(5/(power(10,precision+1)))));
          number:=strtofloat(copy(tmp,0,length(tmp)-1));
        end;
        1 :
        begin
          s:='0.';
          for i:=1 to (precision-1) do
          begin
            s:=s+'0';
          end;
          s:=s+'1';
          if precision<intFloatCount then
          begin
            number:=strtofloat(copy(strnumber,0,intIntCount+1+precision))+strtofloat(s);
          end;
        end;
        2 :
        begin
          number:=strtofloat(copy(strnumber,0,intIntCount+1+precision));
        end;
      end;
      number:=intZFFlag*number;
      result:=number;
    end;
      

  11.   

    seleron
     
    (旺旺贝贝) 
    也出手了呀