To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.cad.devOpen lugnet.cad.dev in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 CAD / Development / 6379
6378  |  6380
Subject: 
Re: Delphi and decimal signs.
Newsgroups: 
lugnet.cad.dev
Date: 
Mon, 20 Aug 2001 19:41:21 GMT
Viewed: 
415 times
  
"Tore Eriksson" <tore.eriksson@mbox325.swipnet.se> skrev i meddelandet
news:3B8154F2.D990585F@mbox325.swipnet.se...
Look at following Delphi code:
  s: string;
  f: double;
  i: Integer;
begin
  f := 3.14; // correct
  f := 3,14; // error (of course...)
  f := StrToFloat('3.14'); // error
  f := StrToFloat('3,14'); // correct
  s := FloatToStr(3.14); // s is assigned '3,14'

Is it the same with an English/American version of Windows and default
national settings?

The problem is that numerical constants in the code must always adhere to the
English notation (decimal point). Conversions between strings and numbers
follow the National settings of Windows (Except the VAL function, which _only_
accepts the English notation!).

This will create a problem if you are reading/writing data files which should
be possible to use on any computer, or if you change your National Settings
often (which I do, just to sort out which programs to keep, and which to throw
away :-)).

It's also a problem if you keep decimal numbers in string properties of the
forms!

(Side note: the same, but larger, problem applies to date constants)

The solution is to stick to one format, irrespective of the Windows settings,
this can be done thus:

In your main.pas (or whatever it's called), at the very end insert the
following:

...
initialization
  DecimalSeparator := '.';
  Application.UpdateFormatSettings := False;
  ...
finalization
  ...
end.

Now all conversions will use/need the decimal point.

If you want to have this format in the data files, but show numbers on screen
as the National Settings suggest, you'll have a much bigger job - you have to
create your own MyIntToStr, MyFloatToStr, MyStrToFloat etc. etc. and hunt
until doomsday through your code before you have found out where to use your
own routines, and where to use the builtin.

(Been there, done that...)

The object oriented way is to embed all the file reading/writing routines in a
class, that does all the necessary checks and conversions and hides
everything, about the implementation, for the users of the class...

HTH,
--
Anders Isaksson, Sweden
BlockCAD:  http://user.tninet.se/~hbh828t/proglego.htm
Gallery:   http://user.tninet.se/~hbh828t/gallery/index.htm



Message has 1 Reply:
  Re: Delphi and decimal signs.
 
(...) screen (...) to (...) your (...) I would attack the problem thusly: Upon application startup, record the locale-specific decimal separator variable into a global variable named UIDecimalSeparator. Create wrappers for FloatToStr and StrToFloat (...) (23 years ago, 20-Aug-01, to lugnet.cad.dev)

Message is in Reply To:
  Delphi and decimal signs.
 
Look at following Delphi code: s: string; f: double; i: Integer; begin f := 3.14; // correct f := 3,14; // error (of course...) f := StrToFloat('3.14'); // error f := StrToFloat('3,14'); // correct s := FloatToStr(3.14); // s is assigned '3,14' Is (...) (23 years ago, 20-Aug-01, to lugnet.cad.dev)

7 Messages in This Thread:



Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR