Whole Numbers-VBForums

1. Does anybody know how to check if a number is whole after being divided by 2 numbers?

2. Use this function

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.

Code:
```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

3. if Numeric=int(Numeric) then 'whole

4. mod

if nbr mod int(nbr) = 0 Then Whole

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

5. 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

Code:
```If (x And 1) = 0 Then
'Even number
Else
'Odd number
End If```
Cheers,

P.

6. Off topic?

7. Kinda off topic

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

Posting Permissions

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

Featured