2

I'm trying to test an value coming from a TEdit, that needs to be converted to a Double. The problem I'm having is that the function TryStrToFloat keeps on returning false, no matter what I do.

    var
      str : String;
      value : Double;
   begin
      str := '950.00';
      value := 0;

     if TryStrToFloat(str, value) then
       result := value
    else
    begin
      showMessage('Not a legal value');
      result := 0;
    end;
  end;

for the example above I supplied the value '950.00' which I then test with the function, but I keep on getting the message that it is "not a legal value". Even if I send '950.00' from the TEdit, I still get the same problem. What am I missing?

Japster
  • 985
  • 6
  • 19
  • 38

1 Answers1

1

You are missing the decimal separator which default to Windows own setting. For example, on the French Windows, the decimal separator is the coma.

Try like this:

var
    str   : String;
    value : Double;
begin
    FormatSettings.DecimalSeparator := '.';
    str   := '950.00';
    value := 0;

    if TryStrToFloat(str, value) then
       ShowMessage('OK')
    else
        ShowMessage('Not a legal value');
end;
fpiette
  • 11,983
  • 1
  • 24
  • 46