Ruby: Change negative number to positive number?

72,988

Solution 1

Using abs will return the absolute value of a number

-300.abs  # 300
300.abs   # 300

Solution 2

Put a negative sign in front of it.

>> --300
=> 300
>> x = -300
=> -300
>> -x
=> 300

Solution 3

Wouldn't it just be easier to multiply it by negative one?

x * -1

That way you can go back and forth.

Share:
72,988

Related videos on Youtube

Shpigford
Author by

Shpigford

Maker. Dabbler. Founder of Baremetrics. I can't stop starting things. Cedar & Sail, Laser Tweets, Founder Chats, Droptune, Rockburg. Bearded.

Updated on July 08, 2022

Comments

  • Shpigford
    Shpigford almost 2 years

    What's the simplest way of changing a negative number to positive with ruby?

    ie. Change "-300" to "300"

  • Sasha Chedygov
    Sasha Chedygov about 14 years
    Just note that if the number is already positive, this will make it negative.
  • Brandon Bodnar
    Brandon Bodnar about 14 years
    my answer is just for negative numbers, if you need to always have the absolute value then this is definitely the better way.
  • Brandon Bodnar
    Brandon Bodnar about 14 years
    Yeah. Clarified that in my comment to the above accepted answer.
  • Stephen Nguyen
    Stephen Nguyen over 10 years
    its either not genius or not not madness.
  • Matthias
    Matthias over 8 years
    I like using this for clarity, as using the - sign can sometimes be easy to miss
  • SRack
    SRack over 7 years
    Worth remembering you can use the shorthand x *= -1 if you're looking to store the new value...
  • Apurva Mayank
    Apurva Mayank about 6 years
    Based of the same principle we can even divide the any negative number by -1. However, I was just wondering what can be the benefit of using it over abs method as mentioned by Yacoby
  • absynthe minded web smith
    absynthe minded web smith about 6 years
    abs returns an absolute value. If that's all you want, as the OP does, it's fine. But what if you need to go back and forth?
  • Andre Figueiredo
    Andre Figueiredo over 4 years
    why negative votes? this turns negative into positive as OP asked. #abs and - are different features with same behavior for asked input/output. If that's because of an undesirable + -> -, it's no more than an assumption, since that was not covered by OP.
  • Andre Figueiredo
    Andre Figueiredo over 4 years
    @ApurvaMayank what's the benefit of using abs over this? OP did not mention, but he could either turn positive to negative as well or keep it positive.. you can't assume one without that being mentioned.
  • Spike0xff
    Spike0xff about 3 years
    This doesn't improve on or add to the previous answers (from years ago) as far as answering the original question, which is specifically about ruby.
  • Yurii Verbytskyi
    Yurii Verbytskyi about 3 years
    In Ruby abs and - methods are both writen in C. One of the main rules for using Ruby: don't try to invent a bicycle, rewriting native C-based methods :) Just click "Show source" to know, how wise people handled that for you) apidock.com/ruby/Integer/abs apidock.com/ruby/Integer/-
  • greyoxide
    greyoxide about 3 years
    I feel like this is a good contribution, While not pinpoint specific DataCure is providing greater context as to how different languages address this issue. In fact the last example givin(c) is a great way to quickly flip the value from negative to positive and back. this could easily be written in ruby as: value * -1. Thank you DataCure, I feel like this was a good first post.