Thomas
Thomas

Reputation: 375

Decimal separator for real numbers in textfiles XE2

The following code gives a real number for Writeln(Outfile,testreal) with a '.' decimal separator even though I have ',' defined in my Windows system. How can I write a number to a text file using Writeln(Outfile,testreal)?

(The FloatToStr method gives ok result as it should.)

procedure TForm1.Button1Click(Sender: TObject);
var Outfile:textfile;
    testreal:single;
    StrDummy : string;

begin
  testreal:=1234.1234;
  assignfile(Outfile,'test_real.txt');
  Rewrite(Outfile);
  StrDummy:='Decimal Separator in Windows: '+GetLocaleChar(GetThreadLocale, LOCALE_SDECIMAL, '.')+#13#10+
            'Decimal Separator in Delphi:  '+DecimalSeparator;
  Writeln(Outfile,StrDummy);
  Writeln(Outfile,testreal);
  Writeln(Outfile,FloatToStr(testreal));
  Closefile(Outfile);
end;

gives

Decimal Separator in Windows: ,
Decimal Separator in Delphi:  ,
 1.23412341308594E+0003                      //Ps. Why is there a beginning space here? 
1234,12341308594

Upvotes: 3

Views: 975

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

Legacy Pascal I/O uses Str to convert floating point values to text. And Str hard codes a decimal separator of ..

I suggest you use a more modern form of I/O. For example the TStreamWriter class which uses FloatToStr to convert floating point to text.

Upvotes: 4

Related Questions