Click to See Complete Forum and Search --> : Whole Numbers


SuperVB
Jan 13th, 2001, 05:06 AM
Does anybody know how to check if a number is whole after being divided by 2 numbers?

[Digital-X-Treme]
Jan 13th, 2001, 05:22 AM
Use this function to check if a number is whole. It converts the variant you pass it to a string, then checks it for a decimal point.


Private Function IsWhole(ByVal Number As Variant) As Boolean

If InStr(1, CStr(Number), ".") Then
IsWhole = False
Else
IsWhole = True
End If

End Function

'Usage
MsgBox IsWhole(32)
MsgBox IsWhole(32.5)


Hope this helps!

Later

kedaman
Jan 13th, 2001, 10:02 PM
if Numeric=int(Numeric) then 'whole

Balder
Jan 15th, 2001, 04:34 AM
if nbr mod int(nbr) = 0 Then Whole

Kedamanīs way seem easier, but I think this would work too.

paulw
Jan 17th, 2001, 06:39 AM
There is another, quite elegant way...

If the number is whole after dividing by two then it is even originally. In Binary representation that means that the last bit will not be set - so - if x is the number then x And 1 will be 0


If (x And 1) = 0 Then
'Even number
Else
'Odd number
End If


Cheers,

P.

kedaman
Jan 17th, 2001, 07:26 AM
Off topic? :rolleyes:

Rh0ads
Feb 5th, 2001, 10:24 AM
Here's a way to find if a number is even or not:

Function Even(Number as long) as boolean
dim a as long
dim b as long

if int(number/2) = number/2 then
even=true
else
even = false
end if
end function