2
votes

My application works on system with regional settings where decimal separator is comma. (Delphi 10.1)

I have set dot as decimal separator for my application .

  Application.UpdateFormatSettings := false;
  FormatSettings.DecimalSeparator := '.';
  Application.UpdateFormatSettings := true;

This works fine for me.

I have used format('%6.3f', 125.365]) function. Exe is on for 24*7 on the system..

In the initial phase like 1 or 2 hours format function returns data properly with dot as decimal separator but later on it changes to local settings comma

say 12,365.

How does dot changes to comma suddenly?

3
Have you looked at the documentation for UpdateFormatSettings? Specifies whether format settings are updated automatically when the user alters the system configuration. I would leave it at False.Tom Brunberg
Do not rely on global settings of FormatSettings. Any part of your program could change the settings. Not thread-safe either. Use the overloaded versions of formatting floats where you pass your own FormatSettings record.LU RD
Alternatively, go with the flow and let your users see numbers in a form they expect.David Heffernan
@LURD: Thanks...I will do so..Is it happens only in Delphi higher versions?poonam
Since Delphi-7 I think.LU RD

3 Answers

5
votes

Do not rely on the global FormatSettings. Use the second overload of Format, which allows you to specify your own TFormatSettings.

If you have a newer version of Delphi, you can directly use TFormatSettings.Invariant:

S := Format('%6.3f', [125.365], TFormatSettings.Invariant)

Or you create a new TFormatSettings:

S := Format('%6.3f', [125.365], TFormatSettings.Create('en-US'));

Or, e.g. in a version that does not have the Invariant or Create methods, you can set the values "by hand", of course. But set them in your own FormatSettings, not in the global one.

3
votes

get the local format setting and change it.

var
  fs:TFormatSettings;
begin
    fs:=TFormatSettings.Create(GetThreadLocale());
    GetLocaleFormatSettings(GetThreadLocale(),fs);
    fs.DecimalSeparator:='.';
end
0
votes

If you don't want people to mess with your stuff, use the kragle.
In this case it is an overload version of Format that accepts a FormatSettings record.

Create a unit like so:

unit DecimalPoint;

interface

uses
  System.SysUtils;

var fs: TFormatSettings;

implementation
initialization
  fs:= FormatSettings;
  fs.ThousandSeparator:= ' ';  <<-- don't forget
  fs.DecimalSeparator:= '.'
end.

And add this unit to the uses where needed.

Now use it like so:

Str:= Format('%6.3f', 125.365],fs);