473,836 Members | 1,499 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 5660
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
6658
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
1464
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
3504
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
25121
by: Kid Programmer | last post by:
What does Double.parseDouble do. I saw it in some code.
1
6762
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
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9672
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
10859
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...
1
10601
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
7794
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
6982
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();...
0
5653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3116
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.