请同志门分别运行下面两段小程序,为什么会出现两种不同的结果?
一:
procedure TForm1.Button1Click(Sender: TObject);
var
a:double;
b:string;
begin
a:=strtofloat(edit1.Text);
showmessage(edit1.Text);
a:=trunc(a*100+0.5);
b:=floattostr(a);
showmessage(b); a:=a/100;
b:=floattostr(a);
showmessage(b);
end;二:
procedure TForm1.Button1Click(Sender: TObject);
var
a:double;
b:string;
begin
a:=strtofloat(edit1.Text);
showmessage(edit1.Text);
a:=a*100+0.5;
b:=floattostr(a);
showmessage(b); a:=trunc(a);
b:=floattostr(a);
showmessage(b); a:=a/100;
b:=floattostr(a);
showmessage(b);end;各种四舍五入的方法为什么对245.725这个数没有用?
我用了各种四舍五入的方法,同志门可以自己试一下:
Label1.Caption:=Format('%.2n',[245.725]);
Label1.Caption:=Format('#.00',[245.725]);
label1.caption:=format('%f',[Trunc(245.725 * 100 +0.5)/100]);
Label1.Caption := RoundTo(245.725, -2);
一:
procedure TForm1.Button1Click(Sender: TObject);
var
a:double;
b:string;
begin
a:=strtofloat(edit1.Text);
showmessage(edit1.Text);
a:=trunc(a*100+0.5);
b:=floattostr(a);
showmessage(b); a:=a/100;
b:=floattostr(a);
showmessage(b);
end;二:
procedure TForm1.Button1Click(Sender: TObject);
var
a:double;
b:string;
begin
a:=strtofloat(edit1.Text);
showmessage(edit1.Text);
a:=a*100+0.5;
b:=floattostr(a);
showmessage(b); a:=trunc(a);
b:=floattostr(a);
showmessage(b); a:=a/100;
b:=floattostr(a);
showmessage(b);end;各种四舍五入的方法为什么对245.725这个数没有用?
我用了各种四舍五入的方法,同志门可以自己试一下:
Label1.Caption:=Format('%.2n',[245.725]);
Label1.Caption:=Format('#.00',[245.725]);
label1.caption:=format('%f',[Trunc(245.725 * 100 +0.5)/100]);
Label1.Caption := RoundTo(245.725, -2);
上面两个小程序中,edit1.text中请输入245.72532
b:=floattostr(a); a:=a*100+0.5;
b:=floattostr(a); 顺序问题。
一: a:=245.725;
a:=a*100+0.5;
a:=trunc(a);
a:=a/100;
b:=floattostr(a);
showmessage(b);
二:
a:=245.725;
a:=trunc(a*100+0.5);
a:=a/100;
b:=floattostr(a);
showmessage(b);
a:=a*100+0.5; ***
a:=trunc(a);
a:=a/100;
二:
a:=245.725;
a:=trunc(a*100+0.5); ***
a:=a/100;还是顺序问题。
不是顺序问题,因为你将a:=245.726,两个程序的结果就一模一样。
我用了各种四舍五入的方法,同志门可以自己试一下:
Label1.Caption:=Format('%.2n',[245.725]);
Label1.Caption:=Format('#.00',[245.725]);
label1.caption:=format('%f',[Trunc(245.725 * 100 +0.5)/100]);
Label1.Caption := RoundTo(245.725, -2);
我用了各种四舍五入的方法,同志门可以自己试一下:
Label1.Caption:=Format('%.2n',[245.725]);
Label1.Caption:=Format('#.00',[245.725]);
label1.caption:=format('%f',[Trunc(245.725 * 100 +0.5)/100]);
Label1.Caption := RoundTo(245.725, -2);