Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a consistent global FormatSettings variable availabe?

Tags:

delphi

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.

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

Example:

FloatToStr(Value, PersistentFormatSettings);
like image 910
markus_ja Avatar asked Nov 02 '22 19:11

markus_ja


2 Answers

No, there is no such variable. You're welcome to define one yourself, though. Declare it in a unit, and then use that unit wherever you need your locale-independent settings.

like image 93
Rob Kennedy Avatar answered Feb 12 '23 14:02

Rob Kennedy


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;
like image 29
Remy Lebeau Avatar answered Feb 12 '23 15:02

Remy Lebeau