Java.math.BigInteger.divideAndRemainder() 方法



說明

java.math.BigInteger.divideAndRemainder(BigInteger val) 返回兩個 BigInteger 組成的陣列,包含 (this / val) 和 (this % val)。

宣告

以下是 java.math.BigInteger.divideAndRemainder() 方法的宣告。

public BigInteger[] divideAndRemainder(BigInteger val)

引數

val - 用於對這個 BigInteger 進行除法運算,並且計算餘數的值。

返回值

此方法返回兩個 BigInteger 組成的陣列:商 (this / val) 是第一個元素,餘數 (this % val) 是最後一個元素。

異常

ArithmeticException - 如果 val 為零。

示例

以下示例展示了 math.BigInteger.divideAndRemainder() 方法的用法。

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      bi1 = new BigInteger("-100");
      bi2 = new BigInteger("3");

      // BigInteger array bi stores result of bi1/bi2
      BigInteger bi[] = bi1.divideAndRemainder(bi2);

      // print quotient and remainder
      System.out.println("Division result");
      System.out.println("Quotient is " + bi[0] );
      System.out.println("Remainder is " + bi[1] );
   }
}

讓我們編譯並執行上述程式,它將生成以下結果 -

Division result
Quotient is -33
Remainder is -1
java_math_biginteger.htm
廣告
© . All rights reserved.