3

下面的代码给出了 Writeln(Outfile,testreal) 的实数,并带有“.”。即使我在 Windows 系统中定义了“,”,也可以使用小数分隔符。如何使用 Writeln(Outfile,testreal) 将数字写入文本文件?

(FloatToStr 方法给出了应有的结果。)

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;

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

1 回答 1

4

Legacy Pascal I/O 用于Str将浮点值转换为文本。并Str硬编码一个小数点分隔符.

我建议您使用更现代的 I/O 形式。例如用于将浮点数转换为文本的TStreamWriterFloatToStr

于 2013-01-10T09:43:20.513 回答