请同志门分别运行下面两段小程序,为什么会出现两种不同的结果?
一:
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); 

解决方案 »

  1.   

    对不起,补充一下:
    上面两个小程序中,edit1.text中请输入245.72532
      

  2.   

    对不起,错了,edit1.text中请输入245.725 才会出现问题。
      

  3.   

    a:=trunc(a*100+0.5);                 
    b:=floattostr(a);  a:=a*100+0.5;
      b:=floattostr(a); 顺序问题。
      

  4.   

    to  zzllabc():其实这两个小程序可以简化一下,得到的结果就不同:  
     一: 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);
      

  5.   

    一: a:=245.725;
          a:=a*100+0.5;           ***
          a:=trunc(a);
          a:=a/100;
     二:
          a:=245.725;
          a:=trunc(a*100+0.5);    ***
          a:=a/100;还是顺序问题。
      

  6.   

    to zzllabc();
    不是顺序问题,因为你将a:=245.726,两个程序的结果就一模一样。
      

  7.   

    不是什么编译原理的问题,其实就是各种四舍五入的方法为什么对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);
      

  8.   

    不是什么编译原理的问题,其实就是各种四舍五入的方法为什么对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);