FormatSettings
is global variable. It can be changed anywhere and all the code that uses it will be affected.
Proper approach for using TFormatSettings
is, not to rely on global FormatSettings
variable and use your own instance when you do formatting. That way you can ensure that some other code beyond your control will not change it and mess up your formatting.
Also, it is good practice that you use your fixed format settings only for storage purposes, and when you present your data to the user you use format settings based on their locale or even better allow them to customize format settings that will be used in your application for presentation purposes.
var
MyFormat: TFormatSettings;
MyFormat := TFormatSettings.Create;
MyFormat.DecimalSeparator:='.';
MyFormat.ThousandSeparator:=' ';
And then you use it in all formatting functions as last parameter.
For functions that use global FormatSettings
variable, the only way you can be sure that formatting will be proper is to set global FormatSettings
variable to your values before you call formatting functions, and restore old values when you are done.
Just for the record StrToFloat
does have overload where you can pass your own settings.
function StrToFloat(const S: string; const AFormatSettings: TFormatSettings): Extended;