Java BigDecimal remainder() Method with Example

BigDecimal Class remainder() method: Here, we are going to learn about the remainder() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 05, 2020

BigDecimal Class remainder() method

Syntax:

    public BigDecimal remainder(BigDecimal divsr);
    public BigDecimal remainder(BigDecimal divsr, MathContext ma_co);
  • remainder() method is available in java.math package.
  • remainder (BigDecimal divsr) method is used to calculate the remainder by using ([this BigDecimal] % divsr).
  • remainder(BigDecimal divsr, MathContext ma_co) method is used to calculate the remainder by using ([this BigDecimal] % divsr) along with rounding based on the given MathContext settings.
  • These methods may throw an exception at the time of calculating the remainder.
    ArithmeticException: This exception may throw when the calculated result is not accurate but the mode of rounding "UNNECESSARY".
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, remainder(BigDecimal divsr),
    • BigDecimal divsr – represents the divisor by which this BigDecimal value is to be divided for calculating remainder.
  • In the first case, remainder(BigDecimal divsr, MathContext ma_co),
    • BigDecimal divsr – Similar as defined in the first case.
    • MathContext ma_co – represents the context setting to use in rounding.

Return value:

In both the cases, the return type of the method is BigDecimal,

  • In the first case, it returns the remainder when this BigDecimal object divides by the given divisor.
  • In the second case, it returns the remainder when this BigDecimal object divides by the given divisor with rounding.

Example:

// Java program to demonstrate the example 
// of remainder() method of BigDecimal

import java.math.*;

public class RemainderOfBD {
    public static void main(String args[]) {
        // Initialize three variables divi1,
        // divi2 and str 
        String divi1 = "120.12";
        String divi2 = "40.55";
        String str = "5";

        // Initialize three BigDecimal objects and
        // one MathContext
        BigDecimal b_dec1 = new BigDecimal(divi1);
        BigDecimal b_dec2 = new BigDecimal(divi2);
        BigDecimal b_dec3 = new BigDecimal(str);
        MathContext ma_co = new MathContext(3, RoundingMode.CEILING);

        System.out.println("remainder(BigDecimal): ");

        // divides this BigDecimal (b_dec1) by the
        // given BigDecimal (b_dec3) and return the BigDecimal
        // object that holds the value of the remainder
        BigDecimal remainder = b_dec1.remainder(b_dec3);
        System.out.println("b_dec1.remainder(b_dec3): " + remainder);

        System.out.println(" ");

        System.out.println("remainder(BigDecimal,MathContext): ");

        // divides this BigDecimal (b_dec2) by the
        // given BigDecimal (b_dec3) based on the given
        // context setting and return the BigDecimal object
        // that holds the value of the remainder
        remainder = b_dec2.remainder(b_dec3, ma_co);
        System.out.println("b_dec2.remainder(b_dec3,ma_co): " + remainder);
    }
}

Output

remainder(BigDecimal): 
b_dec1.remainder(b_dec3): 0.12
 
remainder(BigDecimal,MathContext): 
b_dec2.remainder(b_dec3,ma_co): 0.55


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.