【发布时间】:2014-01-07 02:57:05
【问题描述】:
当我编译并运行以下代码时,它会抛出以下异常:
未处理的异常:System.FormatException:指定的格式“X”无效 在 System.NumberFormatter.NumberToString(System.String 格式,System.Globalization.NumberFormatInfo nfi)[0x00000] 中:0 在 System.NumberFormatter.NumberToString(System.String 格式,十进制值,IFormatProvider fp)[0x00000] in :0 在 System.Decimal.ToString(System.String 格式,IFormatProvider 提供程序)[0x00000] 中:0 在 System.Decimal.ToString (System.String 格式) [0x00000] in :0 在 Program.Main()
using System;
class Program
{
static void Main()
{
decimal result = 1454787509433225637;
//both these lines throw a formatexception
Console.WriteLine(result.ToString("X"));
Console.WriteLine(string.Format("{0:x}", result));
}
}
为什么会这样?根据https://stackoverflow.com/a/74223/1324019,这应该可以正常编译(并输出“14307188337151A5”)。
【问题讨论】:
-
另一个问题使用术语十进制作为基数为 10 的数字。它使用的是格式值
int而不是decimal。 -
在示例中,他们使用的是 int 变量。我认为您将十进制数据类型与将十进制(整数)转换为十六进制时使用的常用术语混淆了。
标签: c# .net hex decimal .net-4.5