TryParse with HexNumber returning wrong value

  • Thread starter Thread starter Greg Wilkerson
  • Start date Start date
G

Greg Wilkerson

Ok, Someone tell me what I'm doing wrong. The statement below is
returning 527.0 in v, instead of the expected 215. Am I missing
something?

double v;

if (Double.TryParse("0d7", NumberStyles.HexNumber,
NumberFormatInfo.InvariantInfo, out v))
{
......
}

Thanks,

Greg
 
Greg,

It's not returning anything for me, actually, it throws an exception.
In .NET 2.0, you can not use the hex modifier on floating point values.

If you are using .NET 2.0, then I would recommend using the TryParse
method on the Int32 structure. If not, then I would call the Parse method,
and catch the exception.

Then I would convert the int to a double through a cast.

Hope this helps.
 
That's interesting. I thought I was on .NET 2.0. I'll have to
upgrade that. Did you get it to work properly using the Int32
structure, or did you try? I'll get my .NET upgraded and try the
Int32.

Thanks.
 
Greg,

It worked fine when I ran this using the TryParse method on Int32.
 
Well, I thought there was a reason I had not upgraded to .NET 2.0; VS
2003 won't use it.

So, I'll just catch a parse exception. Th'at's probably all the
TryParse does, anyway.

On a side note, I did think it was odd to provide a hex conversion for
a double.

Thanks for you help.

Greg
 
Greg,

Actually, the TryParse does not just catch the exception. The whole
point of the TryParse method is to reduce the overhead involved with having
to catch an exception if the string can not be parsed.
 
Back
Top