Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Running delphi program with different setting for number format

Posted on 2014-01-09
18
Medium Priority
?
5,559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +2
18 Comments
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 200 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 28

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 100 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 840 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 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 100 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 28

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression
Suggested Courses

604 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