Delphi 小数点分隔符问题

poo*_*nam 2 delphi format decimal

我的应用程序适用于具有区域设置的系统,其中小数点分隔符为逗号。(德尔福 10.1)

我已将点设置为我的应用程序的小数点分隔符。

  Application.UpdateFormatSettings := false;
  FormatSettings.DecimalSeparator := '.';
  Application.UpdateFormatSettings := true;
Run Code Online (Sandbox Code Playgroud)

这对我来说很好用。

我用过format('%6.3f', 125.365])函数。Exe 在系统上 24*7 全天候运行..

在初始阶段,如 1 或 2 小时格式函数以点作为小数分隔符正确返回数据,但稍后更改为本地设置逗号

12,365

点怎么突然变成逗号了?

Rud*_*uis 5

不要依赖全局FormatSettings。使用 的第二个重载Format,它允许您指定自己的TFormatSettings.

如果您有较新版本的 Delphi,则可以直接使用TFormatSettings.Invariant

S := Format('%6.3f', [125.365], TFormatSettings.Invariant)
Run Code Online (Sandbox Code Playgroud)

或者你创建一个新的TFormatSettings

S := Format('%6.3f', [125.365], TFormatSettings.Create('en-US'));
Run Code Online (Sandbox Code Playgroud)

或者,例如在没有InvariantorCreate方法的版本中,您当然可以“手动”设置值。但是将它们设置在你自己的FormatSettings,而不是全局的。