delphi xe5 StrToFloat failure changing ThousandSeparator to ','

别来无恙 提交于 2019-12-10 13:43:45

问题


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;

回答1:


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])



回答2:


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.



来源:https://stackoverflow.com/questions/25828219/delphi-xe5-strtofloat-failure-changing-thousandseparator-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!