Java.math.BigDecimal.remainder() Method



Description

The java.math.BigDecimal.remainder(BigDecimal divisor) method returns a BigDecimal whose value is (this % divisor).

The remainder is given by this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)). This is not the modulo operation i.e the result can be negative.

Declaration

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

public BigDecimal remainder(BigDecimal divisor)

Parameters

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

Return Value

This method returns the remainder when BigDecimal object is divided by the divisor i.e, this % divisor.

Exception

ArithmeticException − If divisor == 0.

Example

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

      // bg2 divided by bg1 gives bg3 as remainder
      bg3 = bg1.remainder(bg2);

      String str = "The remainder is " + bg3;

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

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

The remainder is 3.54
java_math_bigdecimal.htm
Advertisements