delphi xe5 StrToFloat故障将ThousandSeparator更改为“,” [英] delphi xe5 StrToFloat failure changing ThousandSeparator to ','
问题描述
我在这里做错了什么?我只想将格式化的字符串转换为双精度并使用作为参数传递给StrToFloat的TFormatSettings。我收到以下异常:
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.
千位分隔符和十进制分隔符是TFormatSettings中的预期值(,和。)
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;
推荐答案
您在这里所做的是正确的,但您偶然发现在Delphi SysUtils单元的TextToFloat(似乎忽略了ThousandSeparator)内部函数的错误(如果不是错误,至少不是非常不一致的行为)中(请参阅 Q92265 遵循决议)...
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])
这篇关于delphi xe5 StrToFloat故障将ThousandSeparator更改为“,”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!