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.
Related videos on Youtube
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, 2022Comments
-
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 about 14 yearsJust note that if the number is already positive, this will make it negative.
-
Brandon Bodnar about 14 yearsmy answer is just for negative numbers, if you need to always have the absolute value then this is definitely the better way.
-
Brandon Bodnar about 14 yearsYeah. Clarified that in my comment to the above accepted answer.
-
Stephen Nguyen over 10 yearsits either not genius or not not madness.
-
Matthias over 8 yearsI like using this for clarity, as using the - sign can sometimes be easy to miss
-
SRack over 7 yearsWorth remembering you can use the shorthand
x *= -1
if you're looking to store the new value... -
Apurva Mayank about 6 yearsBased 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 about 6 yearsabs 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 over 4 yearswhy 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 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 about 3 yearsThis 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 about 3 yearsIn Ruby
abs
and-
methods are both writen inC
. One of the main rules for using Ruby: don't try to invent a bicycle, rewriting nativeC
-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 about 3 yearsI 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.