Board index » delphi » StrToFloat fails

StrToFloat fails

Hello,
I have a very trivial problem:

Delphi 6, Windows 2000

procedure TForm1.Button1Click(Sender: TObject);
var dtemp: double;
    strtemp: string;
begin
  strtemp := '1,0';
  dtemp := StrToFloat(strtemp);
  dtemp := dtemp;
end;

StrToFloat does not work, the value for dtemp is not changed at all. The
line thereafter (dtemp := dtemp, introduced as target for F4) is skipped.

Anyone got an idea, why ???

Helge

 

Re:StrToFloat fails


Does it work if you change a line to
strtemp := '1.0';
??

I assume the line dtemp := dtemp; would be removed by the compiler if you
have optimization on.
--
Eddie

Mailto: eddie AT pcug DOT org DOT au
s...@above.for.real.address

"Helge C. Moog" <m...@grs.de> wrote in message
news:a2ogut$3lp$1@mserver1.grs.de...

Quote
> Hello,
> I have a very trivial problem:

> Delphi 6, Windows 2000

> procedure TForm1.Button1Click(Sender: TObject);
> var dtemp: double;
>     strtemp: string;
> begin
>   strtemp := '1,0';
>   dtemp := StrToFloat(strtemp);
>   dtemp := dtemp;
> end;

> StrToFloat does not work, the value for dtemp is not changed at all. The
> line thereafter (dtemp := dtemp, introduced as target for F4) is skipped.

> Anyone got an idea, why ???

> Helge

Re:StrToFloat fails


In article <a2ogut$3l...@mserver1.grs.de>, "Helge C. Moog" <m...@grs.de> writes:

Quote
>StrToFloat does not work, the value for dtemp is not changed at all. The
>line thereafter (dtemp := dtemp, introduced as target for F4) is skipped.

I think you will find that its the decimal point character which is fazing it.
StrToFloat uses the global variable DecimalSeparator as the assumed decimal
point. You most likely have to have code which try's StrToFloat and if it fails
then change a comma or a full stop to the DecimalSeparator character and try's
again.

The DecimalSeparator is derived from MS Locale information which is set at
install time to your national characteristic. You can however change it within
your program.

Alan Lloyd
alangll...@aol.com

Re:StrToFloat fails


Quote
>   strtemp := '1,0';
>   dtemp := StrToFloat(strtemp);

You must use system decimal separator in strtemp.
It is specified in the Windows Control Panel (International Setting).
Use DecimalSeparator for know decimal separator in your system.

  if DecimalSeparator='.' then
    strtemp := '1.0'
  else
    strtemp := '1,0';

Re:StrToFloat fails


I got it:

it seems that the compiler checks whether dtemp is used anywhere after
useage of StrToFloat. As I wanted to check the program up the actual point,
there was no code yet thereafter, where dtemp was used. When I proceeded to
develope my program (and operations were done with dtemp) the phenomenon
vanished.

Thanks all.

Helge

Other Threads