Java BigInteger longValue()方法
java.math.BigInteger.longValue() 将这个BigInteger转换为一个长值。如果该函数返回的值过大,无法装入长值,那么它将只返回低阶64位。这种转换有可能会丢失BigInteger值的整体大小的信息。这个方法也可以返回相反符号的结果。
语法 :
public long longValue()
返回: 该方法返回一个长值,代表该BigInteger的长值。
示例
输入: BigInteger1=3214558191
输出: 3214558191
解释: BigInteger1.longValue()=3214558191.
输入: BigInteger1=32145535361361525377
输出: -4747952786057577855
解释: BigInteger1.longValue()=-4747952786057577855. 这个BigInteger对于longValue来说太大了 longValue,所以它返回低64位。
例1:下面的程序说明了BigInteger类的longValue()方法
// Java program to demonstrate longValue() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigInteger objects
BigInteger b1, b2;
b1 = new BigInteger("3214553537");
b2 = new BigInteger("76137217351");
// apply longValue() method
long longValueOfb1 = b1.longValue();
long longValueOfb2 = b2.longValue();
// print longValue
System.out.println("longValue of "
+ b1 + " : " + longValueOfb1);
System.out.println("longValue of "
+ b2 + " : " + longValueOfb2);
}
}
输出
longValue of 3214553537 : 3214553537
longValue of 76137217351 : 76137217351
例子2:当返回的long对于long值来说太大。
// Java program to demonstrate longValue() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigInteger objects
BigInteger b1, b2;
b1 = new BigInteger("32145535361361525377");
b2 = new BigInteger("7613721535372632367351");
// apply longValue() method
long longValueOfb1 = b1.longValue();
long longValueOfb2 = b2.longValue();
// print longValue
System.out.println("longValue of "
+ b1 + " : " + longValueOfb1);
System.out.println("longValue of "
+ b2 + " : " + longValueOfb2);
}
}
输出
longValue of 32145535361361525377 : -4747952786057577855
longValue of 7613721535372632367351 : -4783767069412450057