Solved

Running delphi program with different setting for number format

Posted on 2014-01-09
18
4,094 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 25

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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 25

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now