当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BigInteger longValue()用法及代码示例


java.math.BigInteger.longValue()将此BigInteger转换为长值。如果此函数返回的值太大而无法容纳长值,则它将仅返回低阶64位。这种转换很可能会丢失有关BigInteger值的整体大小的信息。此方法还可以返回带有相反符号的结果。

用法:

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:当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() Docs



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger longValue() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。