Search code examples
vbscript

VBScript : checking if the user input is an integer


Within a VBScript, I need to make sure the user inputs a integer.

Here is what I have now :

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) Then
       WScript.Echo "Integer"
    Else
       WScript.Echo "Not an integer"
    End If
End if

The problem is that CLng() doesn't test if my number is an integer : the number is converted anyway.

Is there a way to check if a number is an integer ?

EDIT :

The suggested answer doesn't work as well for me. Here is a new version of my 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 Number = CLng(Number) Then
      WScript.Echo "Integer"
   Else
      WScript.Echo "Not an integer"
   End If
End if

and here is the output :

U:\>cscript //nologo test.vbs
Enter an integer number :
12
Not an integer

U:\>cscript //nologo test.vbs
Enter an integer number :
3.45
Not an integer

Solution

  • 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