4
votes

I have a datasnap server and client. The Server-system has decimal seperator of ".". I don't know what my clients will have so i change it in my program.

FormatSettings.DecimalSeparator:='.';
FormatSettings.ThousandSeparator:=' ';

Now when i print from my client the FormatSettings are restored to Default.

  Printer.ActivePrinter.SelectDPI(600,600);
  //formatsettings.Decimalseperator is now ","

What is this behaviour? Is this a bug?

Bonus Q : How do you maintain the same formatsettings for client and server?

1

1 Answers

5
votes

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;