Java BigDecimal divideAndRemainder method example

In the example method of divideAndRemainder(BigDecimal divisor, MathContext mc) returns 'quotient' as per context settings defined in MathContext class object 'mc'.

Java BigDecimal divideAndRemainder method example

In the example method of divideAndRemainder(BigDecimal divisor, MathContext mc) returns 'quotient' as per context settings defined in MathContext class object 'mc'.

Java BigDecimal divideAndRemainder method example

Java BigDecimal divideAndRemainder method example

     

In the example method of divideAndRemainder(BigDecimal divisor, MathContext mc) returns 'quotient'  as per context settings defined in MathContext class object 'mc'.  

Method divides this.object value by the value of object passed. Method return type is an bigdecimal array of length two(2). This array contains the method generated results. At array index zero(array[0]) method generated first result, 'quotient' is located & at array index one(array[1])  method generated second result, 'remainder' is located. 

The first result 'quotient' will always possess integer format. This means method will only generate the integer  part, i.e. the portion of 'quotient' before decimal. Method will generate the remainder also as the second(array[1]) element of bigdecimal array[]. 

Method throws Arithmetic Exception when the divisor value is equal to zero.

Note : Using context settings makes no effect in the generating results as methods neglects decimal part of the answer, thus no decimal part therefore no rounding behavior required hence no context settings required.

Syntax for using the method:
public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc)

bigdecimal_objectName = dividendName.divideAndRemainder(divisor, mc)

Java_divideAndRemainder_BigDecimal_divisor_MathContext_mc.java

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

public class Java_divideAndRemainder_BigDecimal_divisor_MathContext_mc {
  public static void main(String args[]) {
  System.out.println("Java BigDecimal class "
  "\n\tdivideAndRemainder(BigDecimal divisor," +
  " MathContext mc) method example\n");
  int int_value = 25;
  long long_value = 78125;

  MathContext mc = new MathContext(0);
  /* Specifying a static object 'DECIMAL128' in object 
  //'mc' contains
  // following precision settings
  // Number of digits to be accepted : 34
 Rounding mode : HALF_EVEN.*/

  mc = MathContext.DECIMAL128;

  BigDecimal obj_0 = new BigDecimal(long_value),
  obj_1 = new BigDecimal(int_value);

  BigDecimal ray[] = obj_0.divideAndRemainder(obj_1, mc);

  System.out.println("BigDecimal Array ray[] result");
  System.out.println("Quotient : " + obj_0.divide(obj_1)
  "\nQuotient's integer part : " + ray[0
  "\nRemainder : " + ray[1]);

  int_value = 10;
  long_value = 466;
  NumberFormat nc = NumberFormat.getInstance();
  nc.setMinimumFractionDigits(3);
  BigDecimal silver = new BigDecimal(long_value),
  surfer = new BigDecimal(int_value)
  galactus[] = silver.divideAndRemainder(surfer);
  System.out.println("\nBigDecimal Array galactus[] result");
  System.out.println("Quotient : " + nc.format(silver.divide(surfer))
  "\nQuotient's integer part : " + galactus[0]
  "\nRemainder : " + galactus[1]);

  }
}

Download the code