Java get negative value of BigInteger

Tags: BigInteger BigInteger negate

In this Java core tutorial we learn how to use the java.math.BigInteger.negate() method to get the negative value of a given BigInteger value.

How to get negative value of a BigInteger object

In the Java program below, we show you how to use the java.math.BigInteger.negate() method to return the negative value of a given BigInteger value.

NegativeBigIntegerExample.java

import java.math.BigInteger;

public class NegativeBigIntegerExample {
    public static void main(String... args) {
        BigInteger value1 = new BigInteger("123456789");
        BigInteger value2 = new BigInteger("-987654321");

        BigInteger negativeValue1 = value1.negate();
        BigInteger negativeValue2 = value2.negate();

        System.out.println("Negative of " + value1 + " is " + negativeValue1);
        System.out.println("Negative of " + value2 + " is " + negativeValue2);
    }
}
The output is:
Negative of 123456789 is -123456789
Negative of -987654321 is 987654321

Happy Coding 😊