Java-math-biginteger-divideandremainder

提供:Dev Guides
移動先:案内検索

Java.math.BigInteger.divideAndRemainder()メソッド

説明

  • java.math.BigInteger.divideAndRemainder(BigInteger val)*は、(this/val)に続いて(this%val)を含む2つのBigIntegerの配列を返します。

宣言

以下は* java.math.BigInteger.divideAndRemainder()*メソッドの宣言です。

public BigInteger[] divideAndRemainder(BigInteger val)

パラメーター

*val* -このBigIntegerが除算される値、および剰余が計算されます。

戻り値

このメソッドは、2つのBigIntegerの配列を返します。商(this/val)は初期要素であり、剰余(this%val)は最終要素です。

例外

*ArithmeticException* -valがゼロの場合。

次の例は、math.BigInteger.divideAndRemainder()メソッドの使用方法を示しています。

package com.finddevguides;

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