When I compile and run the below code, it throws the following exception:
Unhandled Exception: System.FormatException: The specified format 'X' is invalid at System.NumberFormatter.NumberToString (System.String format, System.Globalization.NumberFormatInfo nfi) [0x00000] in :0 at System.NumberFormatter.NumberToString (System.String format, Decimal value, IFormatProvider fp) [0x00000] in :0 at System.Decimal.ToString (System.String format, IFormatProvider provider) [0x00000] in :0 at System.Decimal.ToString (System.String format) [0x00000] in :0 at 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));
}
}
Why does this happen? According to https://stackoverflow.com/a/74223/1324019 this should compile fine (and output "14307188337151A5").