如何进行四舍五入使12.449=13,每一位都要进行四舍五入

解决方案 »

  1.   

    function f(x:real):integer;
    var s:string;
        i:integer;
    begin
      s:=FloatToStr(x);
      i:=length(s);  while (s[i-1]<>'.')and(i>0) do   //小数点后的每一位四舍五入
        begin
          if s[i]>='5' then
          s[i-1]:=Chr(Ord(s[i-1])+1);
          i:=i-1;
        end;
      if s[i]>='5' then
          s[i-2]:=Chr(Ord(s[i-2])+1);
       result:=Trunc(StrToFloat(s));
    end;
      

  2.   

    var  x:Real;  Y:integer;
    begin
       x:=12.449;
       y:=ceil(12.449);
    end;
      

  3.   

    Function RealRound(R : Real) : Int64;
    var
      i : integer;
    begin
      i := 100000000;
      while i>=10 do begin
        R := Round(R*i)/I + 0.000000000001;
        i := i DIV 10;
      end;
      Result := Round(R);
    end;为什么要加0.000000000001,因为调用Round(14.5)时得到结果是14,如果是14.500000000001得到就是15,晕得很哈。
    以上函数只能处理小数点后8位小数
      

  4.   

    To fanhongbin(Richhero)
    你看看Ceil的实现过程
    function Ceil(const X: Extended): Integer;
    begin
      Result := Integer(Trunc(X));
      if Frac(X) > 0 then
        Inc(Result);
    end;
    只要有小数就加进位,不是四舍五入