Home > Computers, Programming, Work > VBScript function IsNumeric

VBScript function IsNumeric

August 18th, 2009 Leave a comment Go to comments

Brilliant.

Last night, we discovered that the VBScript function “IsNumeric” is a little more clever than you’d like, but there’s also no simpler version either.

Consider the following scenario.

We have the user input in a bit of text on screen. We need to process that (client-side), and part of that processing needs to find out if it is a number or not (i.e. if it’s a number, do this, otherwise, do that.)

Last night, we encountered a problem with the word “65d1” – clearly, it’s not a number (the “d” gives that away,) however, IsNumeric says that it is. If you CInt(“65d1”) you get 650. WTF?

A little more testing reveals the following:

CInt("65d0")  => 65
CInt("65d1")  => 650
CInt("65d2")  => 6500
CInt("65d-1") => 6

So obviously that “d” refers to “*(10 ^ following number)” – however, I can’t find a reference for that.

A bit of googling turns up this page , which shows that adding a space at the front doesn’t stop it from being classed as numeric.

The only real clue to what is going on is to read, and interpret, the MSDN reference page which states:

Returns a Boolean value indicating whether an expression can be evaluated as a number.

And there-in lies the answer – that expression can be evaluated as a number, not that it is a number.

Fortunately, there is an implementation listed on the first page which shows you an alternative way to get the job done.

I’m now wondering if there are other things that you can enter that will affect an number in that way. I tried

CInt("6*5")

but that just errored. I’m wondering whether “e” has a similar effect.

Update: Yes, “e” (at first glance) appears to have an identical effect to “d”.

CInt("65D1") => 650
CInt("65D1") => 6500
CInt("65E1") => 650
CInt("65E1") => 6500
  1. No comments yet.
  1. No trackbacks yet.