Results 1 to 7 of 7

Thread: Function to reverse integer?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Function to reverse integer?

    Hi

    Is there a function to reverse an integer?

    For example ReverseInt(24) would return -24 or ReverseInt(-10) would return 10.

    Thanks.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Function to reverse integer?

    See if this helps,
    Code:
    Private Function ReverseInt(Value As Integer) As Integer
    
        If Value < 0 Then
            ReverseInt = Abs(Value)
        Else
            ReverseInt = -Value
        End If
    
    End Function

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Function to reverse integer?

    I've never heard of that as the 'reverse' of a number, the 'negative' is the usual way to describe it.

    The way to do it is to simply use a - sign, eg:
    Code:
    MyVariable = 24  'set initial value
    
    MyVariable = -MyVariable  '"reverse" it

    edit: hmm.. it seems I was quite slow, but at least I provided a different (and in this case simpler) solution!

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Function to reverse integer?

    I've always used the ABS() command hehe, so what's it good for then?

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Function to reverse integer?

    The Abs function returns the absolute (always positive) version of the number, whether it was already positive or not (so -10 and 10 would both give 10).

    When/how that would be useful depends on what you are doing.. just like getting the negative of a number (as we did here) is.

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Function to reverse integer?

    Quote Originally Posted by si_the_geek
    whether it was already positive or not
    Oh ya thats right, I need more coffee.

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Function to reverse integer?

    Si's solution is obviously the best, but this appeals to my warped mind (only applies to whole numbers)
    Code:
    intI = Not intI -1

Posting Permissions

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



Click Here to Expand Forum to Full Width