请看一下,我想将一个real48类型保留小数点后两位有效数字,并进行四舍五入。代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var i:real48;
begin
i:=2.069999999;
i:=round(i*100)/100;
showmessage(floattostr(i));
end;结果居然是2.6999999999971怎么会这样啊?
请帮忙!
procedure TForm1.Button1Click(Sender: TObject);
var i:real48;
begin
i:=2.069999999;
i:=round(i*100)/100;
showmessage(floattostr(i));
end;结果居然是2.6999999999971怎么会这样啊?
请帮忙!
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;
var i:real;
begin
i:=2.069999999;
i:=(int)(i*100 + 0.5)/100.0;
showmessage(floattostr(i));
end;
不过和cpu的运算精度也有关系
begin
if Value>=0 then
Result:=Trunc(Value+0.5)
else
Result:=Trunc(Value-0.5);
end;
real:=1.9585555654687456.........;
用str(real:10:2,str_real)可以解决.