467,892 Members | 1,879 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,892 developers. It's quick & easy.

double.Parse(double.MaxValue.ToString()) yields an Exception

"double.Parse(double.MaxValue.ToString())" yields the following Exception:

Value was either too large or too small for a Double.
at System.Number.ParseDouble(String value, NumberStyles options,
NumberFormat
Info numfmt)
at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info)
at System.Double.Parse(String s)
...

Even double.Parse((double.MaxValue -1).ToString()) states the same error
message.

That's completly illogical. Is this an error or my misunderstanding?
Nov 17 '05 #1
  • viewed: 5279
Share:
5 Replies
Hello,

Problem is caused by rounding in the default implementation of ToString
method. In the example you gave, method creates a string presentation of a
number rounded to a value that is larger than the largest double number
allowed. So, when you try to parse that string, it throws an exception. You
can check this comparing following two outputs:

Console.WriteLine(double.MaxValue.ToString());
Console.WriteLine(double.MaxValue.ToString("E20")) ;

Therefore this code:

double.Parse(double.MaxValue.ToString("E20"))

will not throw the exception.

Regards,

Julijan
"Markus Kling" <Ma*********@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
"double.Parse(double.MaxValue.ToString())" yields the following Exception:

Value was either too large or too small for a Double.
at System.Number.ParseDouble(String value, NumberStyles options,
NumberFormat
Info numfmt)
at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info) at System.Double.Parse(String s)
...

Even double.Parse((double.MaxValue -1).ToString()) states the same error
message.

That's completly illogical. Is this an error or my misunderstanding?

Nov 17 '05 #2
Julijan Sribar wrote:
Problem is caused by rounding in the default implementation of ToString
method. In the example you gave, method creates a string presentation of a
number rounded to a value that is larger than the largest double number
allowed. So, when you try to parse that string, it throws an exception. You
can check this comparing following two outputs:

Console.WriteLine(double.MaxValue.ToString());
Console.WriteLine(double.MaxValue.ToString("E20")) ;


An alternative format specified to use is "R", which is the round-trip
specifier - it always preserves enough information to complete the
format/parse cycle.

Jon

Nov 17 '05 #3
Sure, that was it. I should drink more coffee.
Thanks a bunch ;)
Nov 17 '05 #4
Just for completion. The recoverable format is not really recoverable. It
does NOT preseve the milliseconds. You have to serialize the DateTime
(timestamp) as long if you want to preserve them.

Markus
"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:11*********************@g49g2000cwa.googlegro ups.com...
Julijan Sribar wrote:
Problem is caused by rounding in the default implementation of ToString
method. In the example you gave, method creates a string presentation of
a
number rounded to a value that is larger than the largest double number
allowed. So, when you try to parse that string, it throws an exception.
You
can check this comparing following two outputs:

Console.WriteLine(double.MaxValue.ToString());
Console.WriteLine(double.MaxValue.ToString("E20")) ;


An alternative format specified to use is "R", which is the round-trip
specifier - it always preserves enough information to complete the
format/parse cycle.

Jon

Nov 17 '05 #5
Markus Kling <ma**********@nospam.nospam> wrote:
Just for completion. The recoverable format is not really recoverable. It
does NOT preseve the milliseconds. You have to serialize the DateTime
(timestamp) as long if you want to preserve them.


The round-trip specifier is a *numeric* specifier. "R" for DateTime
means RFC1123.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Bjørn Augestad | last post: by
4 posts views Thread by Kid Programmer | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.