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


Java Guava BigIntegerMath divide()用法及代碼示例


Guava BigIntegerMath類的除法(BigInteger p,BigInteger q,RoundingMode模式)會返回p除以q的結果,並使用指定的RoundingMode進行舍入。

用法:

public static BigInteger divide(BigInteger p,
                                BigInteger q,
                                RoundingMode mode)

參數:此方法采用以下參數:


  • p:BigInteger股息
  • q:BigInteger除數
  • mode:用於計算p和q的除法的舍入模式。

返回值:此方法返回p除以q的結果,使用指定的RoundingMode進行舍入。

異常:如果q == 0或mode == UNNECESSARY並且a不是b的整數倍,則該方法引發ArithmeticException。

Enum RoundingMode

枚舉常量 描述
CEILING 舍入模式向正無窮大舍入。
DOWN 舍入模式向零舍入。
FLOOR 舍入模式向負無窮大舍入。
HALF_DOWN 除非兩個鄰居等距,否則舍入模式將朝“nearest neighbor”舍入。
HALF_EVEN 舍入模式向“nearest neighbor”舍入,除非兩個鄰居都等距,在這種情況下,向偶數鄰居舍入。
HALF_UP 除非兩個鄰居等距,否則舍入模式將朝“nearest neighbor”舍入。
UNNECESSARY 舍入模式可以斷言所請求的操作具有準確的結果,因此不需要舍入。
UP 舍入模式從零舍入。

以下示例說明了BigIntegerMath.divide()方法:

範例1:

// Java code to show implementation of 
// divide(BigInteger p, BigInteger q, RoundingMode mode) 
// method of Guava's BigIntegerMath class 
  
import java.math.*; 
import com.google.common.math.BigIntegerMath; 
  
class GFG { 
  
    // Driver code 
    public static void main(String args[]) 
    { 
        BigInteger dividend1 = BigInteger.valueOf(55); 
        BigInteger divisor1 = BigInteger.valueOf(10); 
  
        // Using divide() 
        // method of Guava's BigIntegerMath class 
        BigInteger 
            quotient1 
            = BigIntegerMath 
                  .divide(dividend1, 
                          divisor1, 
                          RoundingMode.HALF_DOWN); 
  
        System.out.println(dividend1 + " divided by "
                           + divisor1 
                           + " with HALF_DOWN rounding mode:"
                           + quotient1); 
  
        BigInteger dividend2 = BigInteger.valueOf(55); 
        BigInteger divisor2 = BigInteger.valueOf(10); 
  
        // Using divide() 
        // method of Guava's BigIntegerMath class 
        BigInteger 
            quotient2 
            = BigIntegerMath 
                  .divide(dividend2, 
                          divisor2, 
                          RoundingMode.CEILING); 
  
        System.out.println(dividend2 + " divided by "
                           + divisor2 
                           + " with CEILING rounding mode:"
                           + quotient2); 
    } 
}
輸出:
55 divided by 10 with HALF_DOWN rounding mode:5
55 divided by 10 with CEILING rounding mode:6

範例2:

// Java code to show implementation of 
// divide(BigInteger p, BigInteger q, RoundingMode mode) 
// method of Guava's BigIntegerMath class 
  
import java.math.*; 
import com.google.common.math.BigIntegerMath; 
  
class GFG { 
  
    // Driver code 
    public static void main(String args[]) 
    { 
  
        try { 
  
            BigInteger dividend1 = BigInteger.valueOf(25); 
            BigInteger divisor1 = BigInteger.valueOf(0); 
  
            // Using divide() 
            // method of Guava's BigIntegerMath class 
            // This should raise "ArithmeticException" 
            // as divisor1 = 0 
            BigInteger 
                quotient1 
                = BigIntegerMath 
                      .divide(dividend1, 
                              divisor1, 
                              RoundingMode.HALF_DOWN); 
        } 
        catch (Exception e) { 
            System.out.println("Exception:" + e); 
        } 
    } 
}
輸出:
Exception:java.lang.ArithmeticException:/ by zero

參考: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#divide-java.math.BigInteger-java.math.BigInteger-java.math.RoundingMode-



相關用法


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