Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript : checking if the user input is an integer

Tags:

vbscript

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
like image 327
Jérôme Avatar asked Oct 07 '09 14:10

Jérôme


People also ask

How do you check if a user entered an integer?

To check if the input string is an integer number, convert the user input to the integer type using the int() constructor. To check if the input is a float number, convert the user input to the float type using the float() constructor.

How do you check if something is an integer in VB?

You could perform the following two tests to be reasonably certain that the input you're getting is an integer: If IsNumeric(childAge) AndAlso (InStr(1, childAge, ".") <> 0) Then fmSecA2 = "Wow!

How do you check if a value is an integer or not?

isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .


4 Answers

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.

like image 194
cogumel0 Avatar answered Oct 21 '22 08:10

cogumel0


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
like image 34
backslash17 Avatar answered Oct 21 '22 09:10

backslash17


cogumel's answer above almost gets it, but failed for me in odd ways. I discovered that it would return true for "5" (in quotes), but not 5 (without quotes). When doing the final comparison you need to convert the original input to string as well, to make it all work reliably. Here it is wrapped in a neat function:

public function is_integer( input )
    is_integer = false
    If IsNumeric(input) Then
        If CStr(CLng(input)) = CStr(input) Then is_integer = true
    End If
end function

I've also tested this with zero (true), negative integers (true), both in and out of quote marks.

like image 35
Stephen R Avatar answered Oct 21 '22 08:10

Stephen R


If you do something like this, it should work:

if Number = CInt(Number) Then

like image 41
kemiller2002 Avatar answered Oct 21 '22 10:10

kemiller2002