VBScript : checking if the user input is an integer
This is very similar to your code:
WScript.Echo "Enter an integer number : "
Number = WScript.StdIn.ReadLine
If IsNumeric(Number) Then
' Here, it still could be an integer or a floating point number
If CLng(Number) = Number Then
WScript.Echo "Integer"
Else
WScript.Echo "Not an integer"
End If
End If
This will actually work:
WScript.Echo "Enter an integer number : "
Number = WScript.StdIn.ReadLine
If IsNumeric(Number) Then
' Here, it still could be an integer or a floating point number
If CStr(CLng(Number)) = Number Then
WScript.Echo "Integer"
Else
WScript.Echo "Not an integer"
End If
End If
Previously, the problem was that you were comparing a string vs an integer which would never evaluate to true.
Now, you take a string, check if it's numeric, transform it to CLng() which will return only the integer part of the number, transform it back to a string and finally compare it against the original string.
If you enter... "asdasD" (or any other non-numeric thing) it doesn't pass the "isNumeric" check.
If you enter "10.5" (as a string) when converted to CLng() you get 10 when then gets converted to "10" and compared against "10.5". Since the strings don't match, it says it's not an integer.
If you enter "10" converted to CLng() it's 10, back to string it's "10" which returns a true when matching it against "10", meaning it is an integer.
A few years too late I know, but I was looking into this myself just now and got puzzled by it. Hope it helps anyone else wondering around like myself.