是否存在一致的全局FormatSettings变量? [英] Is there a consistent global FormatSettings variable availabe?

查看:101
本文介绍了是否存在一致的全局FormatSettings变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个全局 FormatSettings 变量,该变量在启动时使用当前的区域OS设置进行初始化。这意味着,当您将字符串转换为数字和签证诗句时,例如xml文件中,然后与其他PC交换该文件。由于无法再将字符串转换为数字,因此可能无法加载此类文件。它取决于 DecimaleSeparator

I know there is a global FormatSettings variable available, which is initialized with the current regional OS settings on startup. That means, when you convert strings to numbers and visa verse, e.g. in an xml file, and you exchange that files with other PCs. It can happen that such a file cannot be loaded, since the strings cannot be convertet back to numbers anymore. It depence on the DecimaleSeparator.

所以我的问题是:是否还有另一个Globel FormatSettings 是否可以使用variabel,我可以将其用于将持久数据存储到文本文件中?

So my question is: Is there another globel FormatSettings variabel available, which I can use for storing persistent data into text file?

示例:

FloatToStr(Value, PersistentFormatSettings);


推荐答案

在现代Delphi版本中,全局<$ c $不建议使用c> FormatSettings 变量(主要是因为它们不是线程安全的)。每个使用格式变量的RTL函数都已重载,以接受可选的 TFormatSettings 记录作为输入。这样一来,您不仅可以使用特定于线程的格式设置,还可以按使用使用自定义格式设置,而不会影响任何其他格式使用。例如:

In modern Delphi versions, the global FormatSettings variable(s) are deprecated (mainly because they are not thread-safe). Every RTL function that uses formatting variables has been overloaded to take an optional TFormatSettings record as input. That allows you to not only use thread-specific formatting settings, but also custom formatting settings on a per-use basis, without affecting any other formatting uses. For example:

var
  Fmt: TFormatSettings;
  S: String;
begin
  Fmt := TFormatSettings.Create; // get default settings
  //
  // or:
  // Fmt := TFormatSettings.Create(SomeLocaleID); // get locale-specific settings
  //
  // or:
  // Fmt := TFormatSettings.Create(SomeLocaleName); // get locale-specific settings
  //

  // customize its fields to use whatever you want...
  Fmt.DecimalSeparator := ...;
  Fmt.ThousandSeparator := ...;

  // now format it...
  S := FloatToStr(Value, Fmt);
end;

这篇关于是否存在一致的全局FormatSettings变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆