tags:

views:

1634

answers:

3

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
+2  A: 

If you do something like this, it should work:

if Number = CInt(Number) Then

Kevin
I prefer to use cLong because cInt cannot support 32 bit integers!
backslash17
you're right CLong will work also. I was just creating a quick example.
Kevin
A: 

another way,

if number > 0 then
...
end if
ghostdog74
What about numbers with decimals? Doubles are not integers but they can be bigger than 0.
backslash17
Numbers (including integers) can also be negative.
Helen
@backslash, then add a line to check for decimals point. Also trivial with negative numbers. just check number <0
ghostdog74
A: 

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
backslash17