use*_*348 1 xml delphi txmldocument regional-settings delphi-10.1-berlin
我正在从XML文件中的节点读取浮点属性值TXMLDocument:
<MyApp_Favorites version="1.0">
Run Code Online (Sandbox Code Playgroud)
......发表声明:
var
ThisRootNode: IXMLNode;
ThisVersion: Single;
// ...
ThisVersion := ThisRootNode.Attributes['version'];
CodeSite.Send('ThisVersion', ThisVersion);
Run Code Online (Sandbox Code Playgroud)
但是,在我的德语系统上,我得到了这个版本值:
ThisVersion = 10,00
...在我的区域设置中,逗号","被定义为小数分隔符,而不是点"." 就像在XML文件中一样.但是使用英语区域设置 - 其中点最可能被定义为小数分隔符设置 - 结果将正确为"1.0".
那么如何确保独立于区域设置,1.0的读取值将始终相同?(将版本值读取为字符串,然后将其转换为float似乎不是一个非常优雅的方法).
使用字符串表示并使用以下方法自行执行转换TFormatSettings.Invariant:
ThisVersion := StrToFloat(ThisRootNode.Attributes['version'], TFormatSettings.Invariant);
Run Code Online (Sandbox Code Playgroud)