【问题标题】:How to assign 01 which should not get converted to 1 with Int32 in C# and gives 01 when refered如何在 C# 中使用 Int32 分配不应转换为 1 的 01 并在引用时给出 01
【发布时间】:2019-03-04 10:08:02
【问题描述】:

-如何在 C# 中使用 Int32 分配不应转换为 1 的 01,并在引用时给出 01。

【问题讨论】:

标签: c# integer int32


【解决方案1】:

这是不可能的。 “01”是某种字符串表示形式,而不是值为“1”的整数的实际值。 .NET 中的数据类型通常没有格式。

使用ToString 你可以format an integer to the format you require。如果这不是你想要的,你应该使用string

【讨论】:

  • 谢谢。我将使用字符串
【解决方案2】:

在这种情况下,您不需要整数而是字符串:

string x = "01"

除非你需要的是二进制数:

in x = 0b01

另一种选择是将值存储在一个整数变量中,并在您需要它之前用左填充零对其进行格式化:

int x = 1;
string formatted = x.ToString("D2");

【讨论】:

  • 谢谢。我将使用字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 1970-01-01
相关资源
最近更新 更多