c# - 格式化 BigInteger 导致空为 0

标签 c#

我正在格式化一个 0 的 BigInteger 值,结果得到一个空字符串。这是预期的行为吗?

System.Numerics.BigInteger value = 0;
string xx = value.ToString("#", System.Globalization.CultureInfo.InvariantCulture);

xx 是 string.Empty 在这两条语句之后。如果我将值设置为 10,我将得到“10”。

最佳答案

是的,这是预期的行为。来自 MSDN :

If the value that is being formatted has a digit in the position where the "#" symbol appears in the format string, that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string.

强调我的

如果您总是想要至少一位数字,则可以使用 0 作为格式字符串:

string xx = value.ToString("0", System.Globalization.CultureInfo.InvariantCulture);

产量:

  • 0“0”
  • 10“10”

关于c# - 格式化 BigInteger 导致空为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723891/

相关文章:

c# - 如何在 C# 中将对象从一个类调用到另一个类

c# - 加载以编程方式编译的程序集失败

c# - 在脚本中迭代对象的属性#

c# - 从旋转值的方向移动 Sprite ?

c# - 为什么 ICustomAttributeProvider.GetCustomAttributes() 返回 object[] 而不是 Attribute[]?

c# - Google Gears SQLite 数据库和 C#

C# 斐波那契数列复制

c# - 无法从 "method group"转换为 "string"

c# - 在 DataGridView 中查找字符串

c# - 无法在 C# 中访问 bool 变量