BigDecimal remainder() method in Java with Examples

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

Note: This is not the modulo operation (the result can be negative).

There are two overloads of remainder method available in Java which is listed below:



  • remainder(BigDecimal divisor)
  • remainder(BigDecimal divisor, MathContext mc)

remainder(BigDecimal divisor)

Syntax:

public BigDecimal remainder(BigDecimal divisor)

Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided for obtaining remainder.

Return value: This method returns a BigDecimal which holds the result (this % divisor).

Exception: The parameter divisor must not be 0 otherwise Arithmetic Exception is thrown.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// remainder() 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
            = "31452678569";
        String input2
            = "2468";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Using remainder() method
        res = a.remainder(divisor);
  
        // Display the result in BigDecimal
        System.out.println(res);
    }
}

chevron_right


Output:

373

remainder(BigDecimal divisor, MathContext mc)

This method is used to calculate the remainder of two BigDecimals whose value is (this % divisor), with rounding according to the context settings. The MathContext settings affect the implicit divide used to compute the remainder. Therefore, the remainder may contain more than mc.getPrecision() digits.

Syntax:

public BigDecimal remainder(BigDecimal divisor, MathContext mc)

Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided and a parameter mc of type MathContext for context settings.

Return value: This method returns a BigDecimal which holds the result (this % divisor).

Exception: The method throws Arithmetic Exception for following conditions:

  • The parameter divisor must not be 0.
  • If mc.precision > 0 and the result requires a precision of more than mc.precision digits.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// remainder() 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 remainder() method
        res = a.remainder(divisor, mc);
  
        // Display the result in BigDecimal
        System.out.println(res);
    }
}

chevron_right


Output:

58

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.