convert double.MinValue to string and back

  • Thread starter Thread starter Pascal
  • Start date Start date
P

Pascal

Why does this not work, and how should i do this convert in stead:

string x = double.MinValue.ToString();
double y = Convert.ToDouble(x);

i get this exception:
An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll
Additional information: Value was either too large or too small for a Double.

Pascal
 
Hello Pascal,

Because
string x = double.MinValue.ToString();
rounds x to -1.79769313486232E+308, whereas the actual minimum value
is -1.7976931348623157E+308. This is a bit higher, so you get the overflow
error when you try to convert it back.

Try using
string x = double.MinValue.ToString("R");
to convert using the 'round trip' (no rounding) format instead.

I hope that helps.
 
Back
Top