How to test for integers in VBScript???

# Thread: How to test for integers in VBScript???

1. Member
Join Date
Dec 1969
Posts
39

## How to test for integers in VBScript???

i know that IsNumeric() function tests if the value is numeric but what if i want to test if the value entered is integer????

2. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: How to test for integers in VBScript???

Assuming you&#039;ve already checked that it&#039;s numeric:<BR><BR>If Int(myVar) = CDbl(myVar) Then<BR><BR>Craig.

3. Senior Member
Join Date
Dec 1969
Posts
402

## easy ..

if instr(cstr(number), ".") = 0 then

4. Member
Join Date
Dec 1969
Posts
39

## thanx alot

its working fine... thanx a million

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## NOT GOOD ENOUGH!

After all, the number string<BR> 1783E45 <BR>is still a valid number! But it doesn&#039;t have any period in it! It&#039;s also mathematically an integer, by the way, but an integer that is way too big to be converted to a VBS LONG or INTEGER data type!<BR><BR>&#060;%<BR>Function GetInteger(str)<BR> &#039; returns null if not an integer<BR> Dim inum<BR> inum = NULL<BR> &#039; don&#039;t even bother testing more if string isn&#039;t numeric<BR> If IsNumeric( str ) Then<BR> On Error Resume Next<BR> inum = CINT( str ) &#039; if this gives error, inum will be unchanged<BR> On Error GoTo 0<BR> If Not ( inum IS NULL ) Then<BR> &#039; and this takes care of a string like "7.34"<BR> If inum &#060;&#062; CDBL( str ) Then inum = NULL<BR> End If<BR> End If<BR> GetInteger = inum<BR>End Function<BR>%&#062;<BR><BR>I think that covers all cases.

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Should have used CLNG, not CINT...

As it is, GetInteger would only get values from -32768 to +32767.<BR><BR>CLNG would extend that to plus or minus two billion.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•