Results 1 to 14 of 14

Thread: how do I get a negative number to become positive?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    is there some code that I can write so that like if I have -3 it auto maticly turns in into 3?
    NXSupport - Your one-stop source for computer help

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    oh yea, I dont want to remove one character and also, if its a positive, I want to turn it into a negative
    NXSupport - Your one-stop source for computer help

  3. #3
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Negative to positive:
    Code:
    Dim Number As Integer
    Number = -5
    Number = Abs(Number)
    Positive to negative:
    Code:
    Dim Number As Integer
    Number = 5
    
    Number = Number - (2 * Number)

  4. #4
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425
    try this:

    dim i as integer

    If i > 0 then
    i = i - (i*2)
    else

    If i < 0 then
    i = i + (i*2)
    else

    end if
    end if

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    NXSupport - Your one-stop source for computer help

  6. #6
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Thanks for the credits.

  7. #7
    Guest
    newnumber = abs(number) * sgn(number) * -1

  8. #8
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Why so much code?

    Change a number from positive to negative and from negative to positive in one simple instruction:
    Code:
    TheNumber = -TheNumber

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    becuase I have an integer

    and I'm not sure if this will work:

    intb = -intb

    but I got what I needed, I needed it for the link above
    NXSupport - Your one-stop source for computer help

  10. #10
    Guest
    That was cool Yonatan.

  11. #11
    Guest
    or you can use Abs

    Code:
    TheNegToPos = Abs(TheNegToPos)
    it gives the absolute value of a number.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    I think thats what I used, read above (oetje's post)
    NXSupport - Your one-stop source for computer help

  13. #13
    Guest
    abs would give only change a negative number to a positive number and not the vice versa.
    Yonatan's code would be the best if the sign is to be changed for both positive and negative numbers.Otherwise abs is just fine.



  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    do you guys read the first posts? for the backward thing, oetje used another medthod.
    NXSupport - Your one-stop source for computer help

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