Jump to content
weabow

Bug with formatfloat in crossplatform ?

Recommended Posts

I have this piece of code :

 

showmessage(formatfloat('###,###.00', 1234.25));

 

It runs fine under Windows, and if I change the decimal separator in the OS, it's taken into account.

 

On Mac, I always have the , (I'm french), even if I change it in the parameters of the OS.

 

An idea ?

Share this post


Link to post

What happens if you explicitly create new TFormatSettings with current locale and call the function with resulting record?

Share this post


Link to post

Well, I'm not familiar with this. I thougth that formatfloat took OS settings into account automatically.

 

Do you mean I have something else to do ?

Share this post


Link to post

Hi,

 

Use System.SysUtils.FormatSettings.DecimalSeparator 

 

one example

 

  if FormatSettings.DecimalSeparator = ',' then
    LText := StringReplace(LText, '.', FormatSettings.DecimalSeparator, [rfReplaceAll])
  else
    LText := StringReplace(LText, ',', FormatSettings.DecimalSeparator, [rfReplaceAll]);

 

br,

m

Share this post


Link to post

Looks fine.

 

But me need is to be synchronized with the plateform settings, maybee France, USA or UK.

 

Sometimes it's a , and in other cases it's a .

 

How can I know which it is on the OS ?

Share this post


Link to post
36 minutes ago, mausmb said:

if FormatSettings.DecimalSeparator = ',' then
    LText := StringReplace(LText, '.', FormatSettings.DecimalSeparator, [rfReplaceAll])
  else
    LText := StringReplace(LText, ',', FormatSettings.DecimalSeparator, [rfReplaceAll]);

This solves all scenarios and local settings :classic_ohmy:

Share this post


Link to post
2 hours ago, mausmb said:

Use System.SysUtils.FormatSettings.DecimalSeparator 

BAD advice. Really bad. Global variables are evil. Moreover, this variable won't update on change either.

4 hours ago, weabow said:

Well, I'm not familiar with this. I thougth that formatfloat took OS settings into account automatically.

 

Do you mean I have something else to do ?

In VCL changes are auto-applied by TForm's internal message handler. FMX doesn't track system changes even on Windows (checked with 10.3). So it's up to you to refresh them.

I'd advice you to use your own instance of FmtSett received by TFormatSettings.Create(SysLocale.DefaultLCID) with all Format functions and refresh them periodically or when they're used. Probably there's more clever option but I'm not aware of any.

Share this post


Link to post

Thanks.

 

I put

TFormatSettings.Create(SysLocale.DefaultLCID);

but the ide doesn't want it.

 

Looking inside help...doesn't help !

Share this post


Link to post

I make it run :

 

fs := TFormatSettings.Create(SysLocale.DefaultLCID);

//fs.DecimalSeparator := '.';

System.SysUtils.FormatSettings := fs;

 

Now it compiles, but no change under MacOs.

 

If I set hardly decimalpoint it runs fine.


But my need is to detect the OS parameter. I think there's a bug.

Share this post


Link to post
2 hours ago, weabow said:

But my need is to detect the OS parameter. I think there's a bug.

Looks like it. Submit a bug report.

Share this post


Link to post
2 hours ago, weabow said:

I make it run :

 

fs := TFormatSettings.Create(SysLocale.DefaultLCID);

//fs.DecimalSeparator := '.';

System.SysUtils.FormatSettings := fs;

That is wrong.  Don't assign the new TFormatSettings instance to the global FormatSettings variable at all (in fact, pretend that global variable doesn't even exist).  Pass the new TFormatSettings instance directly to FormatFloat() instead, eg:

fs := TFormatSettings.Create(SysLocale.DefaultLCID);
// customize fs as needed...
ShowMessage(FormatFloat('###,###.00', 1234.25, fs));

 

  • Like 1

Share this post


Link to post
18 hours ago, Fr0sT.Brutal said:

use your own instance of FmtSett received by TFormatSettings.Create(SysLocale.DefaultLCID) with all Format functions and refresh them periodically or when they're used

Repeat

Share this post


Link to post

Stupid question, is it possible that Win and Macos have different format setting ?

Should be standard in both, but maybe isnt.

Edited by Rollo62

Share this post


Link to post
52 minutes ago, Rollo62 said:

Stupid question, is it possible that Win and Macos have different format setting ?

Should be standard in both, but maybe isnt.

It depends on the locale rather than the os

Share this post


Link to post
5 hours ago, Rollo62 said:

Stupid question, is it possible that Win and Macos have different format setting ?

Well, they're obtained differently, since they're different operating systems. The problem here is either in the call to CFLocaleCopyCurrent or CFLocalGetValue, which is what Delphi currently uses to obtain the decimal separator. The decimalSeparator method of NSLocale gives the correct result.

Edited by Dave Nottage
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×