472,981 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

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
5 5589
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
3
by: Dave Stewart | last post by:
I recently wrote my first Vb.net application, or at least my first complex app since moving up from vb6. When run from the VS.NET IDE, the program shows no errors and runs fine. When the output exe...
2
by: Samuel R. Neff | last post by:
I'm using a quasi open-source project and am running into an exception in double.Parse which is effectively this: double.Parse(double.MinValue.ToString()) System.OverflowException: Value was...
4
by: Kid Programmer | last post by:
What does Double.parseDouble do. I saw it in some code.
1
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.