BigInteger longValueExact() in Java

longValueExact(): This method is available in java.math.BigInteger class of Java.

Syntax:

long java.math.BigInteger.longValueExact()

This method converts this BigInteger to a long, checking for lost information. If the value of this BigIntegeris out of the range of the long type, then an ArithmeticException is thrown.

Returns: this BigInteger converted to a long.

Throws: ArithmeticException - if the value of this will not exactly fit in a long.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.longValueExact() = > It returns 1234.

Approach

Java

import java.math.BigInteger;

public class BigIntegerlongValueExact {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.longValueExact());
    }
}

Output:

1234

No comments:

Post a Comment