📌  相关文章
📜  Java番石榴 | IntMath.divide(int, int, RoundingMode) 方法与示例

📅  最后修改于: 2022-05-13 01:54:27.945000             🧑  作者: Mango

Java番石榴 | IntMath.divide(int, int, RoundingMode) 方法与示例

Guava 的 IntMath 类的divide(int p, int q, RoundingMode mode)方法接受三个参数,计算第一个参数除以第二个参数的结果,根据第三个参数指定的舍入模式进行舍入。

句法:

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

参数:该方法有 3 个参数,其中pq是整数, mode是指定的舍入模式。

返回值:该方法返回第一个参数除以第二个参数,根据第三个参数指定的舍入方式进行舍入。

异常:如果出现以下情况,该方法会抛出ArithmeticException

  • q == 0,或
  • mode == UNNECESSARY 并且 p 不是 q 的整数倍。

枚举舍入模式

Enum ConstantDescription
CEILINGRounding mode to round towards positive infinity.
DOWNRounding mode to round towards zero.
FLOORRounding mode to round towards negative infinity.
HALF_DOWNRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
HALF_EVENRounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
HALF_UPRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
UPRounding mode to round away from zero.

下面的例子说明了上述方法的实现:

示例 1:

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

示例 2:

// Java code to show implementation of
// divide(int p, int q, RoundingMode mode)
// method of Guava's IntMath class
  
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    static int findDiv(int dividend,
                       int divisor,
                       RoundingMode mode)
    {
        try {
            // Using divide(int p, int q,
            // RoundingMode mode)
            // method of Guava's IntMath class
            int quotient
                = IntMath.divide(dividend,
                                 divisor,
                                 RoundingMode.HALF_DOWN);
  
            // Return the answer
            return quotient;
        }
        catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        int dividend = 32;
        int divisor = 0;
  
        try {
  
            // Function calling
            findDiv(dividend, divisor,
                    RoundingMode.HALF_EVEN);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
java.lang.ArithmeticException: / by zero

参考 :
https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#divide-int-int-java.math.RoundingMode-