Java.math.BigDecimal.divideToIntegralValue() Method



Description

The java.math.BigDecimal.divideToIntegralValue(BigDecimal divisor, MathContext mc) returns a BigDecimal whose value is the integer part of (this / divisor). Since the integer part of the exact quotient does not depend on the rounding mode, the rounding mode does not affect the values returned by this method.

The preferred scale of the result is (this.scale() - divisor.scale()). An ArithmeticException is thrown if the integer part of the exact quotient needs more than mc.precision digits.

Declaration

Following is the declaration for java.math.BigDecimal.divideToIntegralValue() method.

public BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc)

Parameters

  • divisor − Value by which this BigDecimal is to be divided.

  • mc − The context to use.

Return Value

This method returns the integer part of this / divisor.

Exception

  • ArithmeticException − If divisor == 0.

  • ArithmeticException − If mc.precision > 0 and the result requires a precision of more than mc.precision digits.

Example

The following example shows the usage of math.BigDecimal.divideToIntegralValue() method.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 3 BigDecimal objects
      BigDecimal bg1, bg2,bg3;

      bg1 = new BigDecimal("99.8");
      bg2 = new BigDecimal("3");

      MathContext mc = new MathContext(2);

      // divide bg1 with bg2 using mc
      bg3 = bg1.divideToIntegralValue(bg2, mc);

      String str = "Integer part of division result using mc is " +bg3;

      // print bg3 value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result −

Integer part of division result using mc is 33
java_math_bigdecimal.htm
Advertisements