Java Get Absolute of BigInteger

Tags: BigInteger BigInteger abs

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

How to get absolute value of a BigInteger object

To get the absolute value we can use the method java.math.BigInteger.abs() which returns a BigInteger whose value is the absolute value of a BigInteger object.

import java.math.BigInteger;

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

        BigInteger absValue1 = value1.abs();
        BigInteger absValue2 = value2.abs();

        System.out.println("The absolute value of " + value1 + " is " + absValue1);
        System.out.println("The absolute value of " + value2 + " is " + absValue2);
The output is:
The absolute value of 123456789 is 123456789
The absolute value of -987654321 is 987654321

Happy Coding 😊