474,005 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

double.Parse(do uble.MaxValue.T oString()) yields an Exception

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

Value was either too large or too small for a Double.
at System.Number.P arseDouble(Stri ng value, NumberStyles options,
NumberFormat
Info numfmt)
at System.Double.P arse(String s, NumberStyles style, NumberFormatInf o info)
at System.Double.P arse(String s)
...

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

That's completly illogical. Is this an error or my misunderstandin g?
Nov 17 '05 #1
5 5690
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.WriteLi ne(double.MaxVa lue.ToString()) ;
Console.WriteLi ne(double.MaxVa lue.ToString("E 20"));

Therefore this code:

double.Parse(do uble.MaxValue.T oString("E20"))

will not throw the exception.

Regards,

Julijan
"Markus Kling" <Ma*********@di scussions.micro soft.com> wrote in message
news:26******** *************** ***********@mic rosoft.com...
"double.Parse(d ouble.MaxValue. ToString())" yields the following Exception:

Value was either too large or too small for a Double.
at System.Number.P arseDouble(Stri ng value, NumberStyles options,
NumberFormat
Info numfmt)
at System.Double.P arse(String s, NumberStyles style, NumberFormatInf o info) at System.Double.P arse(String s)
...

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

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

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.WriteLi ne(double.MaxVa lue.ToString()) ;
Console.WriteLi ne(double.MaxVa lue.ToString("E 20"));


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.co m> schrieb im Newsbeitrag
news:11******** *************@g 49g2000cwa.goog legroups.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.WriteLi ne(double.MaxVa lue.ToString()) ;
Console.WriteLi ne(double.MaxVa lue.ToString("E 20"));


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**********@n ospam.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.co m>
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
6717
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 variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
3
1469
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 is run(this is a windows application) on my development pc, the exe runs fine as well. When the application is deployed on any other machine with .net framework 1.1, it generates an unhandled exception prioir to loading anything. The setup project...
2
3513
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 either too large or too small for a Double. at System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info)
4
25143
by: Kid Programmer | last post by:
What does Double.parseDouble do. I saw it in some code.
1
6776
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 "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1041) at java.awt.Container.add(Container.java:365) at orderingsystem.OrderingSystem.<init>(OrderingSystem.java:261) at...
0
10263
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11980
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11520
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11806
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8584
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3885
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.