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

Re: Bug in Double.MinValue? by Jon

Jon
Fri Jan 25 03:08:58 CST 2008

On Jan 25, 8:54 am, Patrik Kruse
<PatrikKr...@discussions.microsoft.com> wrote:
> 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

Re: Bug in Double.MinValue? by PatrikKruse

PatrikKruse
Fri Jan 25 03:30:00 CST 2008

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



"Jon Skeet [C# MVP]" wrote:

> On Jan 25, 8:54 am, Patrik Kruse
> <PatrikKr...@discussions.microsoft.com> wrote:
> > 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
>