how to check whether a number is an integer?

A

André

Hello,

I need to know whether the result of a division is a integer or not.
dim x =60
dim y = 3

if x/y is a integer then
....
else
....
end if

How can i know that?
Thanks
André
 
M

Mythran

André said:
Thanks

"Theo Verweij" <[email protected]> schreef in bericht
If x/y = x\y then
'Is an integer
else
'is not
end if

I would actually use Mod (modulus) to determine if a division result
(remainder) is 0.

Example:

Dim x As Integer = 12
Dim y As Integer = 6
Dim isInt As Boolean = x Mod y = 0

Console.WriteLine( _
"Remainder of {0} / {1} = {2} : Integral = {3}", _
x.ToString(), _
y.ToString(), _
(x / y).ToString(), _
(x Mod y = 0).ToString() _
)

The last parameter (x Mod y = 0) is the part that I would use :) So:

If x Mod y = 0 Then
' x / y results in a 0 remainder, meaning an int result (given that the
result falls within the scope of an integer and not a long.
Else
' x / y results in a non-0 remainder, meaning a floating point result.
End If

HTH :)

Mythran
 
P

pilgrim

Hello,

I need to know whether the result of a division is a integer or not.
dim x =60
dim y = 3

if x/y is a integer then
...
else
...
end if

How can i know that?
Thanks
André
If x Mod y = 0 then
' it's an integer
else
'it's not an integer
endif
 
H

Herfried K. Wagner [MVP]

André said:
I need to know whether the result of a division is a integer or not.
dim x =60
dim y = 3

if x/y is a integer then
...
else
...
end if

'If Int(x) = x Then...'
 
C

Cor Ligthert [MVP]

Andre,

Put option strict on in top of your program, than it will divide only
doubles using the non integer divide.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top