ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Java BigDecimal divideAndRemainder() Method with Example

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

BigDecimal Class divideAndRemainder() method

Syntax:

    public BigDecimal add(BigDecimal val);
    public BigDecimal add(BigDecimal val, MathContext ma_co);
  • divideAndRemainder() method is available in java.math package.
  • divideAndRemainder(BigDecimal divsr) method is used to get an array of BigDecimal of two elements (the quotient and the remainder), it first divides by using divideToIntegralValue() followed by the result of the remainder on the two values of “BigDecimal” type manipulated.
  • divideAndRemainder(BigDecimal divsr, MathContext ma_co) method is used to get an array of BigDecimal of two elements (the quotient and the remainder), it first divides by using divideToIntegralValue() followed by the result of the remainder on the two values of “BigDecimal” type manipulated 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 given parameter holds the value 0 or when the result is not accurate and set the rounding mode "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, divideAndRemainder(BigDecimal divsr),
    • BigDecimal divsr – represents the divisor by this BigDecimal value is to be divided and generate remainder.
  • In the first case, divideAndRemainder(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[], it returns an array of BigDecimal of two element , the first element is the quotient and last element is remainder.

Example:

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

import java.math.*;

public class DivideAndRemainderOfBD {
    public static void main(String args[]) {
        // Initialize three variables divi1,
        // divi2 and str 
        int divi1 = 120;
        int divi2 = 4;
        String str = "5.6";

        // 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("divideAndRemainder(BigDecimal): ");
        
        // divides this BigDecimal (b_dec1) by the
        // given BigDecimal (b_dec2) and return the BigDecimal[]
        // of two values (Quotient, Remainder)
        BigDecimal[] div_rem = b_dec1.divideAndRemainder(b_dec2);
        System.out.println("Quotient div_rem[0]: " + div_rem[0]);
        System.out.println("Remainder div_rem[1]: " + div_rem[1]);
        System.out.println(" ");

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

        // divides this BigDecimal (b_dec1) by the
        // given BigDecimal (b_dec3) based on the given context setting
        // and return the BigDecimal[] of two values (Quotient, Remainder)
        div_rem = b_dec1.divideAndRemainder(b_dec3, ma_co);
        System.out.println("Quotient div_rem[0]: " + div_rem[0]);
        System.out.println("Remainder div_rem[1]: " + div_rem[1]);
    }
}

Output

divideAndRemainder(BigDecimal): 
Quotient div_rem[0]: 30
Remainder div_rem[1]: 0
 
divideAndRemainder(BigDecimal,MathContext): 
Quotient div_rem[0]: 21
Remainder div_rem[1]: 2.4
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.