Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to use DecimalSeparator to force Floattostr/Strtofloat functions to use a decimal point

Tags:

delphi

Currently, I'm setting DecimalSeparator to a '.' in each procedure which uses these functions.

It would be much easier to set this globally at the start of the program but I found Delphi seems to periodically set this back to the current locale.

I need to make sure that a decimal point is used for all conversions no matter which country the program is used in as this is the standard for this type of program and all files structure and communication protocols, numeric displays in forms/edits etc are required to be formatted in this way.

I've been told in another thread that using decimalseparator is not the correct way to do it but I was not given any alternatives. The other threads concerning this subject that I've read don't seem to offer any formative guidance or are overly complex.

Is there a simple 'correct' way to do this ?

like image 423
Andy k Avatar asked Mar 21 '12 22:03

Andy k


1 Answers

Yes, the DecimalSeparator global variable might be changed by the RTL during runtime, which caused a lot of headache for me a few years ago before I realised this.

The thing is that DecimalSeparator is updated by the RTL when the Windows decimal separator is changed, for instance, using the Control Panel. This might seem like a rather small problem. Indeed, how often does the end user change the system's decimal separator?

The big issue is that the DecimalSeparator variable is updated (according to the system setting) every time you switch user (in Windows). That came as a surprise to me. That is, if your system setting uses a comma (',') as the decimal separator, and you set DecimalSeparator := '.' at application startup, then DecimalSeparator will revert to a comma if you switch user (and you'll notice that when you switch back).

You can tell the RTL not to update the decimal separator by

Application.UpdateFormatSettings := false;

At any rate, there are better alternatives to DecimalSeparator, as discussed in other answers and comments.

like image 122
Andreas Rejbrand Avatar answered Sep 27 '22 22:09

Andreas Rejbrand