Man*_*eld 0 .net c# hex decimal .net-4.5
当我编译并运行以下代码时,它会抛出以下异常:
未处理的异常:System.FormatException:指定的格式"X"在System.NumberFormatter.NumberToString(System.String format,System.Globalization.NumberFormatInfo nfi)[0x00000]中无效,其中:0处于System.NumberFormatter.NumberToString(System.String)格式,十进制值,IFormatProvider fp)[0x00000] in:0表示System.Decimal.ToString(System.String格式,IFormatProvider提供程序)[0x00000] in:0 at System.Decimal.ToString(System.String format)[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));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?根据/sf/answers/5195641/,这应该编译好(并输出"14307188337151A5").