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);
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With