Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi xe5 StrToFloat failure changing ThousandSeparator to ','

What am I doing wrong here? I simply want to convert a formatted string to a double and using the TFormatSettings passed in as a parameter to StrToFloat. I get the following exception:

  '3,332.1' is not a valid floating point value.  

The thousand separator and decimal separator are the expected values (',' and '.') in TFormatSettings.

procedure TForm2.Button1Click(Sender: TObject);
var
  FS: TFormatSettings; 
  S: String;
  V: double;
begin
  FS:= TFormatSettings.Create; 
  codesite.Send('ThousandSeparator', FS.ThousandSeparator);  //correct ','
  codesite.Send('DecimalSeparator', FS.DecimalSeparator);    //correct '.'
  S := '3,332.1';
  try
    V := StrToFloat(S, FS);
  except on E: Exception do
    ShowMessage(e.Message);
  end;
  CodeSite.Send('S', S);
  CodeSite.Send('V', V);
end;
like image 866
Mike Stephenson Avatar asked Sep 13 '14 22:09

Mike Stephenson


2 Answers

This behaviour is as designed. From the documentation, with my emphasis:

Use StrToFloat to convert a string, S, to a floating-point value. S must consist of an optional sign (+ or -), a string of digits with an optional decimal point, and an optional mantissa. The mantissa consists of 'E' or 'e' followed by an optional sign (+ or -) and a whole number. Leading and trailing blanks are ignored.

The DecimalSeparator global variable or its TFormatSettings equivalent defines the character that is used as a decimal point. Thousand separators and currency symbols are not allowed in the string. If S does not contain a valid value, StrToFloat raises an EConvertError exception.

So it is a mistake to pass a string containing a thousands separator to this function.

like image 85
David Heffernan Avatar answered Oct 27 '22 22:10

David Heffernan


What you are doing here is correct, but you stumbled in what it seems a bug(if not a bug at least a not very consistent behaviour) of the TextToFloat(it seems that it ignores ThousandSeparator) internal function of Delphi SysUtils unit(take a look at the Q92265 to follow resolution ) ...

As a workaround you can try removing the group separator, in this way :

StringReplace('3,332.1', ',', '', [rfReplaceAll])
like image 42
aleroot Avatar answered Oct 27 '22 22:10

aleroot