I was writing some code to test numeric boundaries for values being inserted into various cells within an application and came across an interesting discovery (at least it's a discovery to me). I set the default minimum and maximum values for a particular method to double.MinValue and double.MaxValue, respectively. If the user attempted to enter a value outside this range a message would be displayed and the value would be set to the minimum or maximum value allowed, depending on which range was exceeded.
What I found out is that the double.TryParse (as well as the double.Parse) method will fail if you attempt to parse out double.MinValue.ToString() or double.MaxValue.ToString(). The decimal.TryParse (within VS2005), as well as decimal.Parse, both succeed when parsing decimal.MinValue or decimal.MaxValue.
Upon further investigation, I noticed something interesting. The MaxValue for a double is returned as "1.79769313486232E+308" when converted to a string. If you print out double.MaxValue in the immediate window, it is returned as 1.7976931348623157E+308. Notice that when it was converted to a string (via ToString) the value has been rounded up which causes the value to exceed a doubles maximum value.
A workaround for this, although not perfect but good enough for most situations, is to drop the final digit (i.e. the "2"). For example:
double.TryParse(double.MaxValue.ToString().Replace("2E", "E"), out value);
There are probably other (more elegant) solutions but I just happened to find this kind of interesting and thought I'd write a post about it since it could cause issues within an application if you are using this approach for testing boundaries.
Well, looks like I'll be teaching an introduction to C# class starting in January
I've been in contracting for over eight years and have learned a lot not only about technology itself but also various ways to implement the technology in business. I hope to be able to use my experiences to add some business relevance to the "techie" stuff. I'll post from time to time over the next semester letting anyone who's interested know how it's going.
I've had several friends ask about the new version of Visual Studio 2005. I've been working with the new version since the first CTP (Community Technology Preview) and have been fairly impressed with the amount of new features and functionality that has been built into the product. There were several issues in the pre-release version but I haven't ran across any of them in the RTM version (yet ).
For anyone that would like to try it out without dropping the $$$ you can get the Express Editions for free[^]. All Express Editions are free for 1 year[^].
It's been a few years since I posted my first (and only) article on Code Project (Creating an Internet Explorer Favorites Control[^]). I've worked on quite a few .NET projects in that time and now have a great deal more experience than I did back then (don't we all?).
I hope to be able to submit at least one more article by the end of the year and have set a personal goal to submit at least one article per quarter starting next year. I hope that by writing articles about topics that interest me it will help me build my .NET skills even further and hopefully even help someone else out in the end
Well, I guess I better get started on that article...
-- modified at 8:33 Tuesday 11th October, 2005
Last Visit: 31-Dec-99 19:00 Last Update: 26-Jan-15 22:23