最近在编程时遇到一个奇怪的现象请看下面的例子:
1>
procedure TForm1.BitBtn1Click(Sender: TObject);
var x, y : extended;
begin
    x :=100.123;
    y :=strtofloat(formatfloat('0.###',1000.12345678));
    if x =y then      showmessage(floattostr(x)+'='+floattostr(y))
    else
      showmessage(floattostr(x)+'<>'+floattostr(y));
end;
运行后的结果是: 100.123<>100.123 ,请问这是怎么回事.请解释其原因.2>
另外Round函数是将一个实数四舍五入转换为一个64位整数.我用下面的代码求出来的值却是 100 而不是101. 请各位大侠 
 showmessage(floattostr(round(100.5)));3>
 在DELPHI中,怎样将一个实数四舍五入到小数点后2位.以上第一种方法除外.

解决方案 »

  1.   

    1
    应该是那样
    2,
    也是对的,round四舍五入,trunc才能得出100
    3,
    我不知道
      

  2.   

    Delphi的四舍五入函数Round有BUG,无法正常工作。
    对于XXX.5的情况,整数部分是奇数,那么会Round Up,偶数会Round Down,例如:
    x:= Round(17.5) = x = 18 x:= Round(12.5) = x = 12 
    请使用下面的函数代替Round:
    function DoRound(Value: Extended): Int64;  procedure Set8087CW(NewCW: Word);
      asm
        MOV     Default8087CW,AX
        FNCLEX
        FLDCW   Default8087CW
      end;const
      RoundUpCW         = $1B32;
    var
      OldCW             : Word;
    begin
      OldCW := Default8087CW;
      try
        Set8087CW(RoundUpCW);
        Result := Round(Value);
      finally
        Set8087CW(OldCW);
      end;
    end;〈超级猛料〉
      

  3.   

    y :=strtofloat(format('%12.3f',[1000.12345678]));
      

  4.   

    1.  y :=strtofloat(formatfloat('0.###',1000.12345678));
                                            这是什么^_^多了一个零2。四舍六入五看前
    3。FormatFloat
      

  5.   

    DELPHI相对于float型或者double等的比较,都不能用等于进行比较
    应该用a1-a2>0.0000000001的方式进行比较,这是delphi自身的处理方式
      

  6.   

    用一下字段的DisplayFormat('###,###,###,###.##',100.1234)试试看吧。
      

  7.   

    1:应是那样;
    2:
    round(100.5)=100
    round(100.51)=101
      

  8.   

    Delphi自带的函数精度控制不是很理想,对财务上1分两分问题经常出现,建议写一函数,把数值转为字符串处理之后,再以数值形式返回!