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

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

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

Guava 的 LongMath 类的divide(long p, long q, RoundingMode mode)方法接受三个参数,计算第一个参数除以第二个参数的结果,按照第三个参数指定的取整模式取整。

句法:

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

参数:该方法有 3 个参数,其中pq是 long, 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(long p, long q, RoundingMode mode)
// method of Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        long dividend1 = 55;
        long divisor1 = 10;
  
        // Using divide(long p, long q,
        // RoundingMode mode)
        // method of Guava's LongMath class
        long quotient1
            = LongMath.divide(dividend1, divisor1,
                              RoundingMode.HALF_DOWN);
  
        System.out.println(quotient1);
  
        long dividend2 = 55;
        long divisor2 = 10;
  
        // Using divide(long p, long q,
        // RoundingMode mode)
        // method of Guava's LongMath class
        long quotient2
            = LongMath.divide(dividend2, divisor2,
                              RoundingMode.CEILING);
  
        System.out.println(quotient2);
    }
}
输出:
5
6

示例 2:

// Java code to show implementation of
// divide(long p, long q, RoundingMode mode)
// method of Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    static long findDiv(long dividend,
                        long divisor,
                        RoundingMode mode)
    {
        try {
            // Using divide(long p, long q,
            // RoundingMode mode)
            // method of Guava's LongMath class
            long quotient
                = LongMath.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[])
    {
        long dividend = 32;
        long 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/LongMath.html#divide-long-long-java.math.RoundingMode-