Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

BigDecimal divide() Method in Java with Examples

  • Last Updated : 17 Jun, 2019

The java.math.BigDecimal.divide(BigDecimal divisor) is used to calculate the Quotient of two BigDecimals. The Quotient is given by (this / divisor). This method performs an operation upon the current BigDecimal by which this method is called and the BigDecimal passed as the parameter.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

There are five overloads of divide method available in Java which is listed below:

  • divide(BigDecimal divisor)
  • divide(BigDecimal divisor, MathContext mc)
  • divide(BigDecimal divisor, RoundingMode roundingMode)
  • divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
  • divide(BigDecimal divisor, int roundingMode)

Note: The method divide(BigDecimal divisor, int roundingMode) is deprecated since Java 9.



divide(BigDecimal divisor)

The Quotient is given by (this / divisor) and whose preferred scale is (this.scale() – divisor.scale()).
Syntax:

public BigDecimal divide(BigDecimal divisor)

Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided for obtaining quotient.
Return value: This method returns a BigDecimal which holds the result (this / divisor).
Exception: If parameter divisor is 0 or the exact quotient does not have a terminating decimal expansion then Arithmetic Exception is thrown.

Below programs is used to illustrate the divide() method of BigDecimal.




// Java program to demonstrate
// divide() method of BigDecimal
  
import java.math.BigDecimal;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values
        String input1
            = "204800000";
        String input2
            = "256";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Using divide() method
        res = a.divide(divisor);
  
        // Display the result in BigDecimal
        System.out.println(res);
    }
}
Output:
800000

Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divide(java.math.BigDecimal)

divide(BigDecimal divisor, MathContext mc)

This method is used to calculate the quotient of two BigDecimals whose value is (this / divisor), with rounding according to the context settings.
Syntax:

public BigDecimal divide(BigDecimal divisor,
                         MathContext mc)

Parameters: This method accepts two parameters:

  • divisor by which this BigDecimal is to be divided
  • mc of type MathContext for context settings.

Return value: This method returns a BigDecimal which holds the result (this / divisor) and rounded as necessary.



Exception: The method throws Arithmetic Exception if the result is inexact but the rounding mode is UNNECESSARY or mc.precision == 0 and the quotient has a non-terminating decimal expansion.

Below programs is used to illustrate the divide() method of BigDecimal.




// Java program to demonstrate
// divide() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values
        String input1
            = "24536482";
        String input2
            = "264";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Set precision to 5
        MathContext mc
            = new MathContext(5);
  
        // Using divide() method
        res = a.divide(divisor, mc);
  
        // Display the result in BigDecimal
        System.out.println(res);
    }
}
Output:
92941

Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divide(java.math.BigDecimal, java.math.MathContext)

divide(BigDecimal divisor, RoundingMode roundingMode)

The Quotient is given by (this / divisor) and whose preferred scale is this.scale(). The specific rounding mode is applied if rounding is needed to generate result with the given scale.
Syntax:

public BigDecimal divide(BigDecimal divisor,
                         RoundingMode roundingMode)

Parameters: This method accepts two parameters:

  • divisor by which this BigDecimal is to be divided
  • roundingMode of type RoundingMode that tells which rounding mode to apply.

Return value: This method returns a BigDecimal which holds the result (this / divisor).
Exception: The method throws Arithmetic Exception if roundingMode is UNNECESSARY and this.scale() is insufficient to represent the result of the division exactly or Divisor is 0.

Below programs is used to illustrate the divide() method of BigDecimal.




// Java program to demonstrate
// divide() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values
        String input1
            = "2453648454542";
        String input2
            = "264";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Using divide() method
        // Using RoundingMode.CEILING
        res = a.divide(divisor, RoundingMode.CEILING);
  
        // Display the result in BigDecimal
        System.out.println(res);
    }
}
Output:
9294122934

Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divide(java.math.BigDecimal, java.math.RoundingMode)

divide(BigDecimal divisor, int scale, RoundingMode roundingMode)

The Quotient is given by (this / divisor) whose preferred scale is specified. If rounding is required to generate a result with the specified scale, the specified rounding mode is applied.
Syntax:

public BigDecimal divide(BigDecimal divisor, 
                         int scale, 
                         RoundingMode roundingMode)

Parameters: This method accepts three parameters:

  • divisor by which this BigDecimal is to be divided
  • roundingMode of type RoundingMode that tells which rounding mode to apply
  • scale which sets the scale of the quotient.

Return value: This method returns a BigDecimal which holds the result (this / divisor).
Exception: The method throws Arithmetic Exception if roundingMode is UNNECESSARY and the specified scale is insufficient to represent the result of the division exactly or Divisor is 0.

Below programs is used to illustrate the divide() method of BigDecimal.




// Java program to demonstrate
// divide() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values
        String input1
            = "2453648454542";
        String input2
            = "264";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Using scale = 4
        int scale = 4;
  
        // Using divide() method
        // Using RoundingMode.CEILING
        res = a.divide(divisor, scale,
                       RoundingMode.CEILING);
  
        // Display the result in BigDecimal
        System.out.println(res);
    }
}
Output:
9294122933.8713

Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divide(java.math.BigDecimal, int, java.math.RoundingMode)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!