Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java math function to convert positive int to negative and negative to positive?

Tags:

java

math

Is there a Java function to convert a positive int to a negative one and a negative int to a positive one?

I'm looking for a reverse function to perform this conversion:

-5  ->  5  5  -> -5 
like image 750
zippy Avatar asked Oct 23 '11 23:10

zippy


People also ask

How do you change positive to negative in Java?

To convert positive int to negative and vice-versa, use the Bitwise Complement Operator.

How do you convert negative integers to positive?

Multiply with Minus One to Convert a Positive Number So you can use the same method in excel to convert a negative number into a positive. All you have to do just multiply a negative value with -1 and it will return the positive number instead of the negative.

How do you convert a negative double to a positive in Java?

You can do it with a division too: num /= -1; .


1 Answers

What about x *= -1; ? Do you really want a library function for this?

like image 58
FailedDev Avatar answered Sep 22 '22 04:09

FailedDev