Whole Numbers-VBForums
Results 1 to 7 of 7

Thread: Whole Numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    13

    Wink

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


  2. #2
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Wink 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
    Digital-X-Treme
    Contact me on MSN Messenger: digital_x_treme@hotmail.com

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    if Numeric=int(Numeric) then 'whole
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Member
    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    37

    mod

    if nbr mod int(nbr) = 0 Then Whole

    Kedamanīs way seem easier, but I think this would work too.
    Balder = Viking God
    VB6/VC++ Enterprise Editions

  5. #5
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    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.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Off topic?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Addicted Member
    Join Date
    Feb 2001
    Location
    Upstate NY
    Posts
    210

    Talking 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
    < o >

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


Click Here to Expand Forum to Full Width

Survey posted by VBForums.