Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Double.MaxValue Casting Problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kevin Blakeley - 15 Feb 2007 16:26 GMT
I have some code that takes Double.MaxValue and converts it to a string, but
when I try to convert it back to a double, it fails.  Shouldn't the value be
able to be converted back to a double without error?  

Here is a simple code sample that demonstrates the problem:

           try
           {
               Double dbl = double.MinValue;
               //String sdbl = dbl.ToString();  // This errors as well
               String str = dbl.ToString("N");

               // This throws   "Value was either too large or too small
for a Double."
               Double dbl2 = Convert.ToDouble(str);
           }
           catch (Exception ex)
           {
               System.Diagnostics.Debug.Write(ex.Message);
           }
Laura T. - 15 Feb 2007 16:58 GMT
Floating point operations are always approximative, conversions of them to
string even worse.
If you need to convert, it might be better to check the inherent limits
(Min/Max/Infinity etc) and handle them as special case, like:

Double dbl2;

if(str!=double.MinValue.ToString())
   dbl2 = Convert.ToDouble(str);
else
   dbl2=double.MinValue

>I have some code that takes Double.MaxValue and converts it to a string,
>but
[quoted text clipped - 18 lines]
>                System.Diagnostics.Debug.Write(ex.Message);
>            }
Peter Bromley - 15 Feb 2007 21:53 GMT
> I have some code that takes Double.MaxValue and converts it to a string, but
> when I try to convert it back to a double, it fails.  Shouldn't the value be
[quoted text clipped - 16 lines]
>                 System.Diagnostics.Debug.Write(ex.Message);
>             }
Perhaps you should try the round-trip format string instead

String str = dbl.ToString("R");

Peter
Kevin Blakeley - 15 Feb 2007 22:01 GMT
Thanks!  That worked like a champ.  I tried a few of the fomatting types but
did not try that one.  I even tested to see if my value was equal to
Double.Minvalue and it evaluated to true.

Thanks again.

> > I have some code that takes Double.MaxValue and converts it to a string, but
> > when I try to convert it back to a double, it fails.  Shouldn't the value be
[quoted text clipped - 21 lines]
>
> Peter

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.