Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is integer!?

Status
Not open for further replies.

y3by

Programmer
Jan 15, 2002
92
0
0
PT
hi,

i need to test if a value in the textbox is INTEGER,

in java i have a routine called is_integer, but in vb i only find isnumeric.

can someone help me, by telling me the name of the routine or by help create one!

thanksssss
:)
 
How about:
Code:
If x - CInt(x) > 0 Then
    ' x is not an Integer
Else
    ' x is an Integer
End If
 
Somewhat simplified:
Code:
    If x <> CInt(x) Then
        'x is not an integer
    Else
        'x is an integer
    End If
 
The best way is:
If TypeName(X) = &quot;Integer&quot; then
'Is Integer
Else
'Not An Integer
End If

OR the long way

Function IsInteger(valu) as Boolean
On Error Goto NotAnInteger
IsInteger=CInt(Valu)=CInt(Valu)
Exit Function
NotAnInteger:
end Function

 
VBGuy
Y3by posted.
&quot;i need to test if a value in the textbox is INTEGER.&quot;
Typename is not going to do anything in this situation.

And
Won't &quot;CInt(Valu)=CInt(Valu)&quot; always return True if it is an integer.
Try
Function IsInteger(valu as String) as Boolean
On Error Resume Next
IsInteger = (CInt(Valu)= Valu)
If Err.Number <> 0 then IsInteger = false
End Function

Compare Code (Text)
Generate Sort in VB or VBScript
 
&quot;Won't &quot;CInt(Valu)=CInt(Valu)&quot; always return True if it is an integer.&quot; should be

Won't &quot;CInt(Valu)=CInt(Valu)&quot; = True for any real number >= -32768 and <= 32767
e.g. CInt(1.5)=CInt(1.5) = True.
Compare Code (Text)
Generate Sort in VB or VBScript
 
Hi,

This is a simple way to do this.



Dim X As Integer
X = txtNum 'txtNum is the textbox where the
'integer is filled in
If X <> txtNum Then
MsgBox &quot;No integer&quot;
Else
MsgBox &quot;Integer&quot;
End If

Hope this helps
 
thanks to you all..

Public Function IsInteger(valor As String) As Boolean
On Error Resume Next
IsInteger = (CInt(valor) = valor)
If Err.Number <> 0 Then IsInteger = False

End Function

this code works but and ther's always a but...

if i pass valor =13.33 it acepts as an integer

why?

once again thanks!
 
The CInt function can handle only integer values.

So the following could test if a number is in an integer range:

If CInt(Text1.text)=CInt(Text1.Text) Then
'is integer
end if


The above if statement will either pass as true or generate an error. This if statment will never return false. CInt, CCur, CStr are all casting functions that will not accept anything out of it's range. Therefore by simply calling the statement you can determine if it is an integer.

Consider the statment you wrote:
IsInteger=(CInt(Valor) = Valor)
This statment will return the same result as
IsInteger=(CInt(Valor) = CInt(Valor))
because VB is doing the type casting (conversion) in the first one. Try IT!! You have to think like a computer.They both will require error trapping however.

You cannot write CInt(32768) without generating an overflow error. So if any value in your text box is out of the integer range, an error will generate.

You could also do:
IsInteger=Val(Valor)>=-32767 and Val(Valor)<=32767
That would require minimal error trapping.

CInt(13.33) = 13, integers are not floating point numbers. The numbers are rounded. If you want to disallow 13.33 as a valid integer in your program then you would do a statement like this:

IsInteger=(CInt(Valor) = Valor) and (Int(Valor)=Valor)


That should help, unless I'm just missing the point of your question. :->
 
Minor point
An integer can = -32768 (H8000)
&quot;CInt Integer -32,768 to 32,767; fractions are rounded. &quot;

y3by
Cint is Internationall Aware.
1.33 is one hundred thirty three in the UK.

What are your Locale settings. Comma and decimal are reversed in locales like the UK.

I get FALSE with this with US locale.

Private Sub Command1_Click()
Dim b As Boolean
Dim s As String
s = &quot;1.33&quot;
b = IsInteger(s)
Debug.Print b
End Sub

Public Function IsInteger(valor As String) As Boolean
On Error Resume Next
IsInteger = (CInt(valor) = valor)
If Err.Number <> 0 Then IsInteger = False

End Function
BUT I would change it to
IsInteger = (CInt(valor) = CDbl(valor)) Compare Code (Text)
Generate Sort in VB or VBScript
 
Small Point:
I don't believe you even need the
If Err.Number = 0 because the IsInteger will return FALSE by Default. The following should be okay.

Public Function IsInteger(valor As String) As Boolean
On Error Resume Next
IsInteger = (CInt(valor) = valor)
End Function

That's a little more streamlined.

The previous post is right, I meant to say -32768 [16 bits]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top