118

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

ie. Change "-300" to "300"

0

4 Answers 4

305

Using abs will return the absolute value of a number

-300.abs  # 300
300.abs   # 300
1
  • 3
    my answer is just for negative numbers, if you need to always have the absolute value then this is definitely the better way. Mar 19, 2010 at 22:41
31

Put a negative sign in front of it.

>> --300
=> 300
>> x = -300
=> -300
>> -x
=> 300
4
  • 8
    Just note that if the number is already positive, this will make it negative. Mar 19, 2010 at 22:40
  • Yeah. Clarified that in my comment to the above accepted answer. Mar 19, 2010 at 22:45
  • 8
    its either not genius or not not madness. Jan 22, 2014 at 22:13
  • 1
    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. Feb 14, 2020 at 19:31
20

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

x * -1

That way you can go back and forth.

5
  • I like using this for clarity, as using the - sign can sometimes be easy to miss
    – Matthias
    Feb 12, 2016 at 10:21
  • 1
    Worth remembering you can use the shorthand x *= -1 if you're looking to store the new value...
    – SRack
    Oct 24, 2016 at 15:33
  • 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 Mar 20, 2018 at 2:55
  • 1
    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? Mar 21, 2018 at 3:45
  • @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. Feb 14, 2020 at 19:35
0

Most programming languages have the ABS method, however there are some that do not Whilst I have not used Ruby before, I am familiar its a framework that runs on PHP

The abs method is available on PHP https://www.php.net/manual/en/function.abs.php

With Ruby the syntax appears slightly different is integer.abs https://www.geeksforgeeks.org/ruby-integer-abs-function-with-example/

But for future reference the abs method is really small to code your self.

here is how in a few different languages:

JavaScript:

function my_abs(integer){
    if (integer < 0){
        return integer * -1;
    }
    return interger;
}

Python:

    def my_abs(integer):
    if (integer < 0):
        return integer * -1
    return integer

c:

int my_abs(int integer){
    if (interger < 0){
        return integer * -1;
    }
    return integer;
}

This means should you ever find yourself with a programming language that doesnt have a built in abs method, you know how to code your own its just simply multiply any negative number by -1 as you would of gathered in my examples

3
  • 1
    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.
    – Spike0xff
    Mar 18, 2021 at 17:29
  • 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/- Mar 18, 2021 at 19:18
  • 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.
    – greyoxide
    Mar 18, 2021 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.