Java 8中引入了java.math.BigInteger.longValueExact()。它是一个内置函数,可将BigInteger的值转换为long并检查丢失的信息。如果BigInteger的值大于9,223,372,036,854,775,807或小于-9,223,372,036,854,775,808;该方法将抛出ArithmeticException,因为BigInteger不能长期使用。
用法:
public long longValueExact()
返回值:此方法返回此BigInteger的long值。
异常:如果BigInteger的值大于9,223,372,036,854,775,807或小于-9,223,372,036,854,775,808;则该方法引发ArithmeticException。因为此范围不适用于远距离。
例:
Input:98169894145 Output:98169894145 Explanation:98169894145 is given as input which is BigInteger and long value of 98169894145 is 98169894145 Input:-65416518651 Output:-65416518651 Explanation:-65416518651 is given as input which is BigInteger and long value of -65416518651 is -65416518651 Input:10000000000000000000 Output:ArithmeticException Explanation:When 10000000000000000000 is tried to convert to long, since 10000000000000000000 > 9,223,372,036,854,775,807 (greater than a long's range), therefore it throws an arithmetic exception.
以下示例程序旨在说明BigInteger类的longValueExact()方法:
程序1:演示正数<9,223,372,036,854,775,807的longValueExact()方法
// Java program to demonstrate longValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big;
big = new BigInteger("98169894145");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
// convert big to the long value
long b1 = big.longValueExact();
// print long value
System.out.println("long converted value:"
+ b1);
}
}
BigInteger value:98169894145 long converted value:98169894145
程序2:演示用于负数> -9,223,372,036,854,775,808的longValueExact()方法
// Java program to demonstrate longValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-65416518651");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
// convert big to the long value
long b1 = big.longValueExact();
// print long value
System.out.println("long converted value:"
+ b1);
}
}
BigInteger value:-65416518651 long converted value:-65416518651
程序3:演示longValueExact()方法用于负数<-9,223,372,036,854,775,808。它将引发算术异常
// Java program to demonstrate longValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-10000000000000000000");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
try {
// convert big to the long value
long b1 = big.longValueExact();
// print long value
System.out.println("long converted value:"
+ b1);
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
BigInteger value:-10000000000000000000 Exception:java.lang.ArithmeticException:BigInteger out of long range
程序4:为了演示正数> 9,223,372,036,854,775,807的longValueExact()方法。它将引发算术异常
// Java program to demonstrate longValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("10000000000000000000");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
try {
// convert big to the long value
long b1 = big.longValueExact();
// print long value
System.out.println("long converted value:"
+ b1);
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
BigInteger value:10000000000000000000 Exception:java.lang.ArithmeticException:BigInteger out of long range
参考: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#longValueExact-
相关用法
- Java BigInteger gcd()用法及代码示例
- Java BigInteger add()用法及代码示例
- Java BigDecimal longValueExact()用法及代码示例
- Java BigInteger divide()用法及代码示例
- Java BigInteger sqrtAndRemainder()用法及代码示例
- Java BigInteger nextProbablePrime()用法及代码示例
- Java BigInteger isProbablePrime()用法及代码示例
- Java 8 BigInteger byteValueExact()用法及代码示例
- Java BigInteger subtract()用法及代码示例
- Java 8 BigInteger shortValueExact()用法及代码示例
- Java 8 BigInteger divideAndRemainder()用法及代码示例
- Java BigInteger multiply()用法及代码示例
- Java BigInteger intValueExact()用法及代码示例
- Java BigInteger mod()用法及代码示例
- Java BigInteger xor()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Java 8 | BigInteger longValueExact() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。