Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

Is there a more efficient way to check if an object can be converted to a number than this:

 

Function IsNumber(input As Object) As Boolean
   Dim out As Double
   Return Double.TryParse(input, 
           System.Globalization.NumberStyles.Number, 
           System.Globalization.NumberFormatInfo.CurrentInfo, out)
End Function

Posted

Well you can always try converting the number in a try/catch. Errors being thrown are a performance hit though, but who knows, the double.TryParse may be using the same technique.

 

If you run some comparission tests let us know how it works out.

Posted
Well you can always try converting the number in a try/catch. Errors being thrown are a performance hit though, but who knows, the double.TryParse may be using the same technique.

 

If you run some comparission tests let us know how it works out.

 

That's what I always do. I think this is quite performant, since:

 

1) as you said, Double.TryParse probably do a try/catch

 

2) the IsNumber() will probably be used most of the time with a number. Maybe 10% will generate a catch(). Depends how you use it, but If you actually expect a number, how many time the user will send a string that is non compatible? The user will send a number most of the time.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...