Java中的BigInteger longValue()方法

这个 JAVA数学大整数。longValue() 将此BigInteger转换为长值。如果此函数返回的值太大,无法放入长值中,那么它将只返回低阶64位。这种转换可能会丢失有关BigInteger值的总体大小的信息。此方法还可以返回符号相反的结果。

null

语法:

public long longValue()

返回: 该方法返回一个long值,该值表示这个BigInteger的long值。

例如:

Input: BigInteger1=3214558191
Output: 3214558191
Explanation: BigInteger1.longValue()=3214558191.

Input: BigInteger1=32145535361361525377
Output: -4747952786057577855
Explanation: BigInteger1.longValue()=-4747952786057577855. This BigInteger is too big for 
longValue so it is returning lower 64 bit.

示例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:当return 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

参考: BigInteger longValue()文档

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享