Java.math.BigDecimal.divide() Method



Description

The java.math.BigDecimal.divide(BigDecimal divisor) returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()).If the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.

Declaration

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

public BigDecimal divide(BigDecimal divisor)

Parameters

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

Return Value

This method returns a BigDecimal object whose value is this / divisor.

Exception

ArithmeticException − If the exact quotient does not have a terminating decimal expansion.

Example

The following example shows the usage of math.BigDecimal.divide() 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("45");
      bg2 = new BigDecimal("5");

      bg3 = bg1.divide(bg2); // divide bg1 with bg2

      String str = "Division result is " +bg3;

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

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

Division result is 9
java_math_bigdecimal.htm
Advertisements