【问题标题】:What is the C# equivalent to Delphi's FormatFloat?什么是 C# 等价于 Delphi 的 FormatFloat?
【发布时间】:2012-09-06 12:54:00
【问题描述】:

我正在将一些 Delphi 代码移植到 C#。我找不到与Delphi的FormatFloat类似的功能。

我在 Delphi 中有这行代码

str := FormatFloat('000', 1);

将字符串'001' 分配给str。注意前导零。

如何在 C# 中实现相同的结果?

【问题讨论】:

  • 请参阅this link 了解左填充零的具体格式。
  • 不行,因为数字需要在它之前添加。这很令人困惑,但在delphi中它被称为FormatFloat,但我将它用于整数(A)但@akton的答案是正确的并且成功了:P
  • @KenWhite,谢谢,已加入书签 ;-)

标签: c# delphi formatting


【解决方案1】:

您将string.Format()custom numeric format strings 一起使用。例如:

int a = 1;
string.Format("{0:000}", a); // returns "001"

【讨论】:

    【解决方案2】:

    你可以使用 ToString() 方法:

    int number = 3;
    string fmt = number.ToString("D3");
    

    字符串变量 fmt 的值为“003”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      相关资源
      最近更新 更多