请看一下,我想将一个real48类型保留小数点后两位有效数字,并进行四舍五入。代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var i:real48;
begin
i:=2.069999999;
i:=round(i*100)/100;
showmessage(floattostr(i));
end;结果居然是2.6999999999971怎么会这样啊?
请帮忙!

解决方案 »

  1.   

    i:=StrToFloat(format('%.2f',[2.069999999]));
      

  2.   

    Delphi的帮助:
    var   S, T: string;begin   Str(1.4:2:1, T);
       S := T + ' rounds to ' + IntToStr(Round(1.4)) + #13#10;
       Str(1.5:2:1, T);
       S := S + T + ' rounds to ' + IntToStr(Round(1.5)) + #13#10;
       Str(-1.4:2:1, T);
       S := S + T + ' rounds to ' + IntToStr(Round(-1.4)) + #13#10;
       Str(-1.5:2:1, T);
       S := S + T + ' rounds to ' + IntToStr(Round(-1.5));
       MessageDlg(S, mtInformation, [mbOk], 0, mbOk);
    end;
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var i:real;
    begin
    i:=2.069999999;
    i:=(int)(i*100 + 0.5)/100.0;
    showmessage(floattostr(i));
    end;
      

  4.   

    同意  cg1120 的
    不过和cpu的运算精度也有关系
      

  5.   

    function Round(Value:Double):Integer;
    begin
      if Value>=0 then
         Result:=Trunc(Value+0.5)
      else
         Result:=Trunc(Value-0.5);
    end;
      

  6.   

    var str_real: string
    real:=1.9585555654687456.........;
    用str(real:10:2,str_real)可以解决.