Does anybody know how to check if a number is whole after being divided by 2 numbers?
Printable View
Does anybody know how to check if a number is whole after being divided by 2 numbers?
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.
Hope this helps!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)
Later
if Numeric=int(Numeric) then 'whole
if nbr mod int(nbr) = 0 Then Whole
Kedaman“s way seem easier, but I think this would work too.
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
Cheers,Code:If (x And 1) = 0 Then
'Even number
Else
'Odd number
End If
P.
Off topic? :rolleyes:
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