views:

297

answers:

2
A: 
Brad
That's a case of abusing exceptions. try..except is expensive. Better use TryStrToFloat in SysUtils.
TOndrej
Thanks didn't even know about that function.
Brad
A: 

While I don't know what is the problem which you faced perhaps is useful for you...

  function CompareFloat(AStr1, AStr2: string): Integer;
  const
    _MAGIC = -1; //or ANY number IMPOSSIBLE to reach

  var
    nInt1, nInt2: extended;

  begin
    nInt1:=StrToFloatDef(AStr1, _MAGIC);
    nInt2:=StrToFloatDef(AStr2, _MAGIC);
    if nInt1 > nInt2 then Result := 1
    else
      if nInt1 = nInt2 then Result := 0
    else
      Result := -1;
  end;

..and another snippet (perhaps much better):

function CompareFloat(aInt1, aInt2: extended): integer;
begin
  Result:=CompareValue(aInt1, aInt2); // :-) (see the Math unit) - also you can add a tolerance here (see the 'Epsilon' parameter)
end;

Besides the rounding which can cause you problems you can see what the format settings are in conversion between string and numbers (you know, the Decimal Point, Thousands Separator aso.) - see TFormatSettings structure in StringToFloat functions. (There are two - overloaded).

HTH,