Home All Groups Group Topic Archive Search About

how to check whether a number is an integer?

Author
27 Oct 2006 4:13 PM
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é

Author
27 Oct 2006 4:17 PM
Theo Verweij
If x/y = x\y then
    'Is an integer
else
    'is not
end if

André wrote:
Show quoteHide quote
> 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é
>
>
Author
27 Oct 2006 4:52 PM
André
Thanks

"Theo Verweij" <tverw***@xs4all.nl> schreef in bericht
news:%231XBiNe%23GHA.4376@TK2MSFTNGP03.phx.gbl...
If x/y = x\y then
'Is an integer
else
'is not
end if

André wrote:
Show quoteHide quote
> 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é
>
Author
27 Oct 2006 6:38 PM
Mythran
Show quote Hide quote
"André" <s**@sdv.sd> wrote in message
news:eahb7ge#GHA.4464@TK2MSFTNGP02.phx.gbl...
> Thanks
>
> "Theo Verweij" <tverw***@xs4all.nl> schreef in bericht
> news:%231XBiNe%23GHA.4376@TK2MSFTNGP03.phx.gbl...
> If x/y = x\y then
> 'Is an integer
> else
> 'is not
> end if
>
> André wrote:
>> 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é
>>
>
>

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
Author
27 Oct 2006 6:54 PM
pilgrim
Show quote Hide quote
On Fri, 27 Oct 2006 18:13:27 +0200, "André" <s**@sdv.sd> wrote:

>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
Author
27 Oct 2006 6:55 PM
Herfried K. Wagner [MVP]
"André" <s**@sdv.sd> schrieb:
> 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...'

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
28 Oct 2006 6:14 AM
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

Show quoteHide quote
"André" <s**@sdv.sd> schreef in bericht
news:Ou8oULe%23GHA.1172@TK2MSFTNGP03.phx.gbl...
> 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é
>