Converts negative number to positive

Asked

Viewed 20,150 times

-2

Is there a native java library that helps me in an elegant way to convert a negative number to positive.

I didn’t mean to do it that way:

public class Main {  

    public static void main(String[] args) {  
        int i1 = 10;  
        int i2 = -i1;  
        int i3 = -i2;  

        System.out.printf("%d\n%d\n%d", i1, i2, i3);  
    }  
}  
  • 7
  • Math.abs(number);

  • 2

    Use good old math, just multiply by -1 int i2 = i1 * (-1) and use a if (i1 < 0) to check if the number is negative.

  • 1

    @Olimonf., just do int i2 = -i1, nay?

  • @Pedrolorentz I’m not very experienced with java, but I think the 3 solutions posted should work, but I don’t know about the performance of each or the right way. So I posted as a comment and no reply :|

  • @Olimonf., in the text of the question he had already used -i1, hehe. As for performance, the multiplication is expected to be slower yes (but I hope the compiler can optimize and transform into the unary operator).

  • 1

    Here has an interesting solution (for C++, but should work in Java as well).

  • 1

    I honestly don’t understand your question. Do you want a way to convert a negative to a simpler positive than to put a negative sign in front of the variable? I think it’s impossible. Maybe what you wanted to ask was not exactly that, if that’s the case please clarify your question.

  • I honestly don’t understand your question. At which point it was not explicit that, I was looking for a form of conversion from positive to negative without changes of signals explicitly. As @bfavaretto said, Math.abs

Show 4 more comments

3 answers

3


The big trick is to use the module mathematical function or absolute value to print what you want. Because it is a fairly common function it is possible to find it in most programming languages in java the same is defined in Math.abs() and abs() is the acronym for absolute in English.

This way your code could use this way:

public class Main {

    public static void main(String[] args) {
        int i1 = -10;
        //Pega o valor absoluto de i1 isto é o módulo do mesmo
        int i2 = Math.abs(i1);
        System.out.println(i1);
        System.out.println(i2);
    }
}

Another way to do this would be through regular expressions, but it would make no sense to use a regular expression and conversions just to pick up the module from a number once this operation would be very costly (although possible):

public class Main {

    public static void main(String[] args) {
        int i1 = -10;
        String regex = "" + i1;
        regex = regex.replaceAll("[^0-9]", "");
        int i2 = Integer.parseInt(regex);
        System.out.println(i2);
    }
}

Note that it’s pretty much the same if you did it with replace:

regex = regex.replaceAll("-", "");

or

regex = regex.substring(1); //retira o primeiro caractere

Although all these methods work does not compensate to create a String object only to make a conversion when Java already provides the function in the Math Library.

2

According to the suggestion of @Olimon F. and solving in just one line

int i2 = -1;
int x = ((i2 < 0) ? -i2 : i2);
System.out.println(x);

// output: 1
  • 4

    That’s exactly what the Math.abs does, but I think I use Math.abs easier to read.

  • Agree with you @Pedrolorentz.

-3

Another solution to the problem:

int N = -3
int R = ((N*2)+N)

The result of R is 3.

  • 1

    Beyond the obvious faults of ;, the theoretical result and what I get running on my machine is quite distinct from what you say you got. For me, R == -9

Browser other questions tagged

You are not signed in. Login or sign up in order to post.