BigInteger divide method in Java with examples| BigDecimal divide tutorials

This post shows how to divide BigInteger with others in java with examples. You can also check my previous posts on the BigInteger class in java.

The same does not work for BigInteger objects. It gives compilation error Operator ’/’ cannot be applied to ‘java.math.BigInteger’, ‘java.math.BigInteger’.

Here is an example using the division operator

import java.math.BigInteger;

public class BigIntegerMultiply {
    public static void main(String[] args) {
        BigInteger operand1= BigInteger.valueOf(2);
        BigInteger operand2= BigInteger.valueOf(2);
        BigInteger result=operand1*operand2;
        System.out.println(result);
    }
}

It provides a multiply method to do multiplication on BigInteger.

Java BigInteger multiply method with example

The divide method returns the result of the multiplication of values.

BigInteger is immutable and the result of methods always returns a new object.

It internally uses an array of integers to do the process, The performance is less compared with Integer multiplication.

Syntax:

    public BigInteger divide(BigInteger val)

Here is an example of a BigInteger divide


import java.math.BigInteger;

public class BigIntegerMultiply {
    public static void main(String[] args) {
        BigInteger divident= BigInteger.valueOf(212312);
        BigInteger divisor= BigInteger.valueOf(2123);
        BigInteger result=divident.divide(divisor);
        System.out.println(result);
    }
}

Output:

100

divide two BigDecimals values into a BigDecimal object

First, Convert BigInteger into BigDecimal using the below code Next, use the divide method and the result is BigDecimal.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigIntegerMultiply {
    public static void main(String[] args) {

        BigDecimal operand1= BigDecimal.valueOf(212312);
        BigDecimal operand2= BigDecimal.valueOf(2123);
        BigDecimal result=operand1.divide(operand2,4, RoundingMode.HALF_UP);
        System.out.println(result);
    }
}

Output:

100.0057