sgiannop
-
Total Posts
:
3
- Scores: 0
-
Reward points
:
0
- Joined: 11/27/2011
-
Status: offline
|
Check for integer input
Monday, November 28, 2011 7:39 AM
( permalink)
Greetings everyone. I want to check with an if statement, if the user input is an integer or not. How do i do this? I 've tried the following: --------------------------------------------------------------------------------------- input = InputBox("Give an integer:") If Int(input) = input Then MsgMox "You gave an integer." End If --------------------------------------------------------------------------------------- But, unfortunately this does not seem to work. Any idea? Thanks.
|
|
|
|
59cobalt
-
Total Posts
:
969
- Scores: 91
-
Reward points
:
0
- Joined: 7/17/2011
-
Status: offline
|
Re:Check for integer input
Monday, November 28, 2011 7:55 AM
( permalink)
If VarType(input) = vbInteger Then MsgBox "You gave an integer."
|
|
|
|
sgiannop
-
Total Posts
:
3
- Scores: 0
-
Reward points
:
0
- Joined: 11/27/2011
-
Status: offline
|
Re:Check for integer input
Monday, November 28, 2011 9:21 AM
( permalink)
Thanks a lot! for the quick answer. 59cobalt If VarType(input) = vbInteger Then MsgBox "You gave an integer." Though this seems to be another right way to check if the input is integer, i still don't get the right result if i give integer as an input. check it for yourself! Any reason?
|
|
|
|
sgiannop
-
Total Posts
:
3
- Scores: 0
-
Reward points
:
0
- Joined: 11/27/2011
-
Status: offline
|
Re:Check for integer input
Monday, November 28, 2011 9:30 AM
( permalink)
If VarType(input) = vbInteger Then MsgBox "You gave an integer." By the way, the reason that the above way fails is different following this way. Now if is executed even whem we give decimal numbers in the input. when we: MsgBox VarType(input) we take as a result the integer 8 in both cases that input is an integer or a decimal. What's wrong anyway?
|
|
|
|
59cobalt
-
Total Posts
:
969
- Scores: 91
-
Reward points
:
0
- Joined: 7/17/2011
-
Status: offline
|
Re:Check for integer input
Monday, November 28, 2011 10:54 AM
( permalink)
That's because InputBox() always returns either a string or Empty. You can use IsNumeric() to check if the string contains a numeric value. However, that will return True for doubles as well. Try this: If IsNumeric(input) Then
If CStr(CInt(input)) = input Then
WScript.Echo "Input is an Integer."
ElseIf CStr(CLng(input)) = input Then
WScript.Echo "Input is a Long."
ElseIf CStr(CDbl(input)) = input Then
WScript.Echo "Input is a Double."
End If
End If
|
|
|
|
ehvbs
-
Total Posts
:
3320
- Scores: 112
-
Reward points
:
0
- Joined: 6/22/2005
- Location: Germany
-
Status: offline
|
Re:Check for integer input
Monday, November 28, 2011 10:55 AM
( permalink)
Using InputBox() as your GUI is a bad idea. If you insist on communicating with your user via vanishing popups, you you should be aware of: - InputBox() returns a string (vbString == 8) or an empty value (vbEmpty == 0) on abort
- the IsNumeric() functions checks whether a string can be converted to a value of numerical (sub) type; this conversion may loose info
- a numerical string can be converted to a double (CDbl()), a double may have no fractional part (double == Fix(double))
- a fixed double can be converted to a integral (sub) type: CByte(), CInt(), CLng() - if it is in the appropriate range
|
|
|
|
ebgreen
-
Total Posts
:
8219
- Scores: 98
-
Reward points
:
0
- Joined: 7/12/2005
-
Status: offline
|
Re:Check for integer input
Tuesday, November 29, 2011 3:48 AM
( permalink)
Here is a quick and dirty function that should handle most cases. Function IsInt(strIn) Dim strFractional if Not IsNumeric(strIn) Then IsInt = False Exit Function End If if InStr(strIn, ".") > 0 Then strFractional = Split(strIn, ".")(1) if CInt(strFractional) > 0 Then IsInt = False Exit Function End If End If IsInt = True End Function
|
|
|
|