Why IsNumeric("5-") is true?

  • Thread starter Thread starter Guest
  • Start date Start date
Tony,

Why not, that is common here, be aware that this method is to check strings
from textboxes

Cor
 
This is a legacy VB/VBA function that returns true if the value *starts*
with something usable (working in conjonction with Val that will properly
convert something that starts with a number).

If you want to be strict on possible values, it is best to use other
functions such as TryParse...
 
Patrice,

That is not how IsNumeric works.

If it did work that way then, for example, IsNumeric("5B") would return
True, but it returns False.

Kerry Moorman
 
Tony said:
Can anybody tell why IsNumeric("5-") is true?

It represents a number just less than 5, like 5+ represents a number just
greater than 5.

For example, you might encounter something like "the limit of 1/x as x->0-
...." (where -> is meant to represent an arrow) which means the limit of 1/x
as x approaches zero from the negative side.

See it written as a superscript at http://mathworld.wolfram.com/Limit.html
(amusing cartoon at end of page, too :-)

Andrew
 
Good point. I probably forgot how it works exactly. The bottom line is that
this function can return true in some cases even if the "number" is not
strictly a number so your best bet is to use another function that does a
more strict checking...

P.S. I finally gave it a try using the following code :

Dim a() As String = {"&h5", "5?", "5 6"} ' Use your own currency
symbol instead of ?
Dim r As Integer
For i As Integer = 0 To UBound(a)
Debug.WriteLine(String.Format("{0},{1},{2}", a(i),
IsNumeric(a(i)), Integer.TryParse(a(i), r)))
Next

Results are :

&h5,True,False
5?,True,False
5 6,True,False

As you can see IsNumeric is not as picky than TryParse (and was already in
VB/VBA so they likely kept this unchanged for compatibility)...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top