Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6004
  • Last Modified:

Running delphi program with different setting for number format

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
is-ol
Asked:
is-ol
  • 6
  • 4
  • 3
  • +2
4 Solutions
 
ThommyCommented:
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
 
ThommyCommented:
0
 
Sinisa VukCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
is-olAuthor Commented:
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
 
Pierre CorneliusCommented:
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
 
Geert GruwezOracle dbaCommented:
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
 
is-olAuthor Commented:
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
 
is-olAuthor Commented:
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
 
Pierre CorneliusCommented:
May I ask why the B grade?
0
 
ThommyCommented:
May I ask why I didn't get any points????
I was the first who suggested setting DecimalSeparator at runtime...
0
 
Sinisa VukCommented:
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
 
is-olAuthor Commented:
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
 
Pierre CorneliusCommented:
You should have awarded an A grade.

Here are some guidelines for future reference:
http://support.experts-exchange.com/customer/portal/articles/481419
0
 
is-olAuthor Commented:
Ok I see. Seems I cannot correct this now. I will heed to the guidelines in future, sorry.
0
 
Pierre CorneliusCommented:
No problem. At least you know for next time.
0
 
is-olAuthor Commented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now