procedure TForm1.Button1Click(Sender: TObject);
var
  Max,Min : real;
begin
  Max := 7.13;
  min := 6.99;
  if (strtofloat(edit1.Text) < min) or (strtofloat(edit1.Text) > max) then
    showmessage('why?');  
end;以上這段code.在edit1中輸入6.99,應該說不會出現'why?'.因為6.99不小於6.99.但實際執行時,會跳出'why?'.
換成下面這種直接跟數字比較,就不會跳出'why?'.不知道是strtofloat函數的問題,還是real這個數據類型的問題.請高手指教!!procedure TForm1.Button1Click(Sender: TObject);
var
  Max,Min : real;
begin
  Max := 7.13;
  min := 6.99;
  if (strtofloat(edit1.Text) < 6.99) or (strtofloat(edit1.Text) > 7.13) then
    showmessage('why?');  
end;

解决方案 »

  1.   

    刚才粘贴代码错了
    估计min的值在计算机内部是表示为6.990000001的,故大之.
      

  2.   

    建议代码如下更改
    var
      Max,Min : real;
    begin
      Max := 7.13;
      min := 6.99;
      edit1.Text := floattostr(min);
      if abs(strtofloat(edit1.Text) - min)>0.00000000001 then// or (strtofloat(edit1.Text) > 7.13) then
        showmessage('why?');
    end;
      

  3.   

    min := 6.99实际的数据应该是6.98999999999999...这是因为精度的问题导致
      

  4.   

    謝謝大家的回復.
    我試過用double類型,7.13和6.99都是正確顯示的.只有用single類型才會顯示為6.9899999999這樣.
    後來我將strtofloat(edit1.Text)先賦給一個real的變數,然後用這個變數和min,max比較,也就是用同類型的數據進行比較.就沒問題了.