Solved

Running delphi program with different setting for number format

Posted on 2014-01-09
18
4,421 Views
Last Modified: 2014-01-16
Hello, we have a Delphi Program (running under windows) that only runs correctly when the OS is set to english number format, i.e. the dot .  as decimal separator. As we are in Germany this means changing the number format every time.

I spent some time trying to convince delphi to use the dot regardless what locale is set but I could not get this working. Then I thought it might be possible to set environment variables before executing the program; I set LANG to "en" but this does not seem to influence the decimal separator. Is there another environment variable for this or another way to run a program with specific language/number settings?

Thank you for a hint,
jc
0
Comment
Question by:is-ol
  • 6
  • 4
  • 3
  • +2
18 Comments
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 50 total points
ID: 39770305
Explicitly set the decimal separator in your application to english version overwriting system settings

procedure TForm1.FormCreate(Sender: TObject);
var
  SystemDecimalSep: char;
begin
  SystemDecimalSep :=  DecimalSeparator;
  DecimalSeparator := '.';  //fix decimal separator to english number format
end;
0
 
LVL 19

Expert Comment

by:Thommy
ID: 39770309
0
 
LVL 26

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 25 total points
ID: 39770328
Changing DecimalSeparator is one way. Second is to make "non-sensitive" application. If you use Delphi "native" components a right way then you are on track. Did you detect places where you have problems with that? Things can be written correctly ... If it is DB application - use DB components, they will post values in db correctly. If you must build sql in code - do number transformations as is shown in second Thommy's post (link).
Post some example of your problematic code here.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:is-ol
ID: 39770356
Hi, thank you all, its not my own code ant it's ~ 10k lines.... I will try to determine where exactly the "wrong" conversion is being done and then get back here.
0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 210 total points
ID: 39776663
what version of delphi are you using?

In Delphi 2009 you can use the global variable DecimalSeparator (declared in SysUtils unit) e.g. DecimalSeparator:= '.';

In Delphi XE3 you can use the global variable FormatSettings.FormatSettings.DecimalSeparator (declared in SysUtils unit) e.g. FormatSettings.DecimalSeparator = '.';

Where? I would suggest putting it in your program file (click on Project, view source).

Take note that these are globals and not thread safe, so if your app uses multi-threading you need to take that into account. In such case it would be better to supply functions using format settings with a variable in each case e.g. (for Delphi XE3):

function SomeValFormattedAsCurr(val: double): string;
var fs: TFormatSettings;
begin
  fs:= TFormatSettings.Create;
  fs.DecimalSeparator:= '-';
  fs.ThousandSeparator:= ',';
  result:= FormatFloat('#,##0.00', val, fs);
end;

Open in new window


Side-note:
Thread safety should not really be an issue as you probably won't be changing the format all the time anyway - probably only once at application startup.
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 25 total points
ID: 39776869
you can set it to what you like and if you have policies running every so often the policy may reset it to what the company dictates as user defaults

unless you deactivate this

on create of mainform:

DecimalSeparator := '.';
Application.UpdateFormatSettings := False;
0
 

Author Comment

by:is-ol
ID: 39777001
Thank you for the hints, unfortunately I could not yet check whether that helps as our internet connection broke down. I hope to finish that tomorrow. Regards,jc
0
 

Author Comment

by:is-ol
ID: 39778963
Yes, that seems to be the solution, now I started to use local FormatSettings and I can read in the data independent from the locale setting. Thank you very much!
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 39779070
May I ask why the B grade?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 39779080
May I ask why I didn't get any points????
I was the first who suggested setting DecimalSeparator at runtime...
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39779100
My hint stay the same. Always try to be independent regarding to a date/number format.
I you app will be international - you will have a problem.
0
 

Author Comment

by:is-ol
ID: 39784683
Sorry, PierreC, I did not use EX much up to now so I am not really familiar with the grades. I thought this is the normal grade for a "good" solution. Thommy, I tried your code but this did not work for me. I set the Decimal separator variable everywhere but only after using  local TFormatSettings in every format statement I got round the issue. Maybe that is due to the specific usage of string conversions in the code. Sinisav, the data in question is coming in from a SQL database with a fixed format. Naturally I only apply the local settings to these data, not to all other values that are and should be locale-dependent. Thank you all for helping!
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 39784831
You should have awarded an A grade.

Here are some guidelines for future reference:
http://support.experts-exchange.com/customer/portal/articles/481419
0
 

Author Comment

by:is-ol
ID: 39784990
Ok I see. Seems I cannot correct this now. I will heed to the guidelines in future, sorry.
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 39785001
No problem. At least you know for next time.
0
 

Author Closing Comment

by:is-ol
ID: 39785851
Since an admin reopened the question I now tried to award points to all who helped. I award PierreC 210 pts as this code piece finally did the trick. Sorry for misinterpreting EE's grade system.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question