Possible ToString() bug

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I've looked around the web a bit and didn't find much luck in locating
a solution. Here the bit of code that I'm working with:

strMyValue = "";

for (double d = double.MinValue; d < double.MaxValue && numPoints <
maxPoints; d+=1.1F)
{
strMyValue = d.ToString();
//Here I hand off the string to be used by another object
}

The strangeness that I get comes in the conversion d's value to a
string. In the debug window, I see this value for d:

-1.7976931348623157E+308

After I call d.ToString(), I receive this value for strMyValue:

"-1.79769313486232E+308"

Somehow the ToString() function managed to round the last few digits
(157 becomes a 2). Normally, this wouldn't be an issue, but since I'm
on the edge of the minimum value for a double, this rounding causes the
object that I had this off to to fail. The way I see it (and I know
these are two seperate things but hang with me), if the debug window
can display a textual representation of d's value, then the ToString()
function should be able to do as good a job.

Why did I ever create this snippet? I am testing a COM object to
determine if the object is able to successfully coerce strings back
into their repective data types. Of course, since the number I'm
representing is now outside the range of a valid double due to the
rounding issue, the coercion fails.

Has anyone else had trouble with the ToString() method? I've tested
some of the other types and, to this point, only doubles have given me
any hassle.

Thanks,
Gary

.



Relevant Pages

  • Re: VB.NET Overloading problem/question
    ... It seems you are trying to make ToString do too much. ... Public Overridable Property Subject As String ... BodyMessage & SubjectBodyMessage would then simply need to add ... Public Overrides Readonly Property HasBody As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Question concerning object-oriented programming
    ... If at the end of the constructor the ... return defaults such as zero, a null string, or null. ... can also throw exceptions from methods. ... toString() in there. ...
    (comp.programming)
  • Re: What if my properties have methods?
    ... > I would like to be able to reference mycust.name.length which would return ... Because .Name returns a String object, you can use any method of the String ... ".tostring" to your types. ... the "how" of OOP in VB.NET, however it does not cover the "why" of OOP. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: CStr() vs. .ToString()
    ... you should use the Visual Basic type conversion functions in ... preference to the .NET Framework methods such as ToString(), ... methods on a string that is "Nothing" (s.ToUpper for instance won't ... use the more object-oriented methods of a ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Arraylist question
    ... ToString virtual method in your PERSON class to return: ... public override string ToString() ... Now you can add the instances of person directly to the combobox which will ... > public int CompareTo ...
    (microsoft.public.dotnet.languages.csharp)