Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indicate if string has numeric value

Tags:

vbscript

I loop over string variable data which may have integer numeric value, like "123". If this string variable has numeric value, I want to indicate it and thought to use some like this:

If IsNumeric(CInt(data)) Then 
    WScript.Echo "Number"
Else
    WScript.Echo "String"
End If

But CInt() raises error every time data variable can't be converted to integer:

Type mismatch: 'CInt'

How can I indicate if string has integer value in vbscript?

like image 383
theta Avatar asked Jul 12 '12 10:07

theta


2 Answers

IsNumeric Function of vb script can be used to determine whether an expression can be evaluated as a number.It returns Boolean value depending upon expression

Please be noted that IsNumeric returns False if expression is a date expression.

Now, in your code you have mistaken that even if it is not number, you trying to convert it into integer

You can use this in your code like this--

If IsNumeric(data) Then       
   WScript.Echo "Number"
Else
   WScript.Echo "String"
End If
like image 183
Amol Chavan Avatar answered Nov 22 '22 14:11

Amol Chavan


only for integers:

If VarType(data) = vbInteger Then
    WScript.Echo "Integer"
Else
    WScript.Echo "Something else"
End If

for numbers:

If IsNumeric(data)
    WScript.Echo "Number"
Else
    WScript.Echo "string"
End If
like image 32
user1519979 Avatar answered Nov 22 '22 14:11

user1519979