FormatSettings

FormatSettings is a global record in Delphi which is used to keep locale-related variables such as DecimalSeparator, CurrencyFormat, CurrencyString, etc. This variable was introduced in Delphi XE. Before Delphi XE, DecimalSeparator, CurrencyFormat, etc. were declared as separate global variables in SysUtils.pas.

The problem with FormatSettings is that it is a global variable which doesn't cope with uniGUI's multithreaded environment. To overcome this problem, developers must ensure that FormatSettings is modified only before sessions are started. It is not safe to modify FormatSettings inside sessions. To provide a safe way to access FormatSettings, uniGUI implements two functions:

function FmtSettings: TFormatSettings;

function PFmtSettings: PFormatSettings;

The function FmtSettings returns a readonly TFormatSettings record which can be used in various RTL functions such as:

FloatToStr(FValue, FmtSettings);

The function PFmtSettings returns a pointer to the TFormatSettings record which allows modifications, for example:

PFmtSettings.CurrencyString := '£';

When called inside a session context, the above functions will return a private copy of the TFormatSettings record which belongs to that session. This private instance of TFormatSettings will be used internally in all uniGUI functions which use parameters in TFormatSettings. However, not all internal DB functions use this private copy. Some DB functions such as TField.AsString use the global FormatSettings, so if you want to use a different FormatSettings for each session you must take this into account. The same is true for 3rd-party database tools which use Delphi's global FormatSettings.

On the other hand, if these functions are called inside the OnCreate event of ServerModule, they will set the global FormatSettings used by all sessions in the application.

The code below will change the global FormatSettings for all sessions in the application:

ServerModule - change global FormatSettings
procedure TUniServerModule.UniGUIServerModuleCreate(Sender: TObject);
begin
  // Change application global format settings
  PFmtSettings.CurrencyFormat  := 0;
  PFmtSettings.CurrencyString  := '';
  PFmtSettings.DateSeparator   := '/';
  PFmtSettings.ShortDateFormat := 'dd/mm/yyyy';
  PFmtSettings.ThousandSeparator := '.';
  PFmtSettings.DecimalSeparator  := ',';
end;

When the above functions are called inside a session context, the returned record is not the global instance of the FormatSettings record which is defined in SysUtils.pas. Instead, it will be a private instance of TFormatSettings record which is held in TUniGUIApplication class. This means that inside a session you should explicitly include FmtSettings as a parameter for RTL functions which use it:

The code below will change the private FormatSettings for the current session only: