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


Java BigInteger intValueExact()用法及代碼示例


Java 8中引入了java.math.BigInteger.intValueExact()。它是一個內置函數,它將BigInteger的值轉換為int並檢查丟失的信息。如果BigInteger的值大於2,147,483,647或小於-2,147,483,648;該方法將拋出ArithmeticException,因為BigInteger不在int範圍內。

用法:

public int intValueExact()

返回值:此方法返回此BigInteger的int值。


異常:如果BigInteger的值大於2,147,483,647或小於-2,147,483,648,則該方法引發ArithmeticException。因為此範圍不適合int範圍。

例:

Input: 4561561
Output: 4561561
Explanation: 4561561 is given as input which is bigInteger
and int value of 4561561 is 4561561

Input: -8546512
Output: -8546512
Explanation: -8546512 is given as input which is bigInteger 
and int value of -8546512 is -8546512

Input: 3000000000
Output: ArithmeticException
Explanation: When 3000000000 is tried to convert to int,
since 3000000000 > 2,147,483,647 (greater than a int's range), 
therefore it throws an arithmetic exception.

以下示例程序旨在說明BigInteger類的intValueExact()方法:

示例1:演示intValueExact()方法為正數

// Java program to demonstrate intValueExact() 
// 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("4561561"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value : "
                           + big); 
  
        // convert big to the int value 
        int b1 = big.intValueExact(); 
  
        // print int value 
        System.out.println("int converted value : "
                           + b1); 
    } 
}
輸出:
BigInteger value : 4561561
int converted value : 4561561

示例2:演示用於負數> -2,147,483,648的intValueExact()方法

// Java program to demonstrate intValueExact() 
// method of BigInteger Class 
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Creating a BigInteger object 
        BigInteger big = new BigInteger("-8546512"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value : "
                           + big); 
  
        // convert big to the int value 
        int b1 = big.intValueExact(); 
  
        // print int value 
        System.out.println("int converted value : "
                           + b1); 
    } 
}
輸出:
BigInteger value : -8546512
int converted value : -8546512

示例3:演示intValueExact()方法的負數

// Java program to demonstrate intValueExact() 
// method of BigInteger Class 
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Creating a BigInteger object 
        BigInteger big = new BigInteger("-3000000000"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value : "
                           + big); 
  
        try { 
            // convert big to the int value 
            int b1 = big.intValueExact(); 
  
            // print int value 
            System.out.println("int converted value : "
                               + b1); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
BigInteger value : -3000000000
Exception: java.lang.ArithmeticException: BigInteger out of int range

示例4:演示intValueExact()方法用於正數> 2,147,483,647。它將引發算術異常

// Java program to demonstrate intValueExact() 
// method of BigInteger Class 
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Creating a BigInteger object 
        BigInteger big = new BigInteger("3000000000"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value : "
                           + big); 
  
        try { 
            // convert big to the int value 
            int b1 = big.intValueExact(); 
  
            // print int value 
            System.out.println("int converted value : "
                               + b1); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
BigInteger value : 3000000000
Exception: java.lang.ArithmeticException: BigInteger out of int range

參考: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#intValueExact–



相關用法


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