Bug in Double.MinValue?

  • Thread starter Thread starter Patrik Kruse
  • Start date Start date
P

Patrik Kruse

This line exectues with an exception:

double d = double.Parse(double.MinValue.ToString());

The error message is "Value was either too large or too small for a Double".

Is this an error in the framwork?

/Patrik
 
This line exectues with an exception:

double d = double.Parse(double.MinValue.ToString());

The error message is "Value was either too large or too small for a Double".

Is this an error in the framwork?

Not quite - it's a bug in how you're formatting it. If you call plain
ToString() there's no guarantee that that value can be parsed, or that
it will be parsed to exactly the same value. The "r" format string
(roundtrip) makes that guarantee, so the following works:

double d = double.Parse(double.MinValue.ToString("r"));

I agree it's counterintuitive, but look at the two strings produced
and you'll see why it happens.

Jon

Jon
 
Thanks Jon,

it is really not intuitive, and I get the value from a SOAP request so the
generation of it is out of my control.

I guess I must hardcode the min value string to be able to recognise the
value received as MinValue (used as null) then... :(

/Patrik
 
Back
Top