當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java 8 BigInteger longValueExact()用法及代碼示例


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-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Java 8 | BigInteger longValueExact() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。