ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

# Java BigDecimal divideToIntegralValue() Method with Example

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

## BigDecimal Class divideToIntegralValue() method

Syntax:

```    public BigDecimal divideToIntegralValue(BigDecimal divsr);
public BigDecimal divideToIntegralValue(BigDecimal divsr, MathContext ma_co);
```
• divideToIntegralValue() method is available in java.math package.
• divideToIntegralValue(BigDecimal divsr) method is used to get a BigDecimal and its value is the non-fractional part of the quotient is calculated by using (this BigDecimal/ BigDecimal divsr) and the scale is calculated by using (this BigDecimal.scale() – BigDecimal divsr.scale()).
• divideToIntegralValue(BigDecimal divsr, MathContext ma_co) method is used to get a BigDecimal and its value is the non-fractional part of the quotient is calculated by using (this BigDecimal/ BigDecimal divsr) along with rounding based on the given MathContext settings.
• These methods may throw an exception at the time of division.
ArithmeticException: This exception may throw when the given parameter holds the value 0 or when ma_co.precision > 0.
• 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, divideToIntegralValue(BigDecimal divsr),
• BigDecimal divsr – represents the divisor by this BigDecimal value is to be divided.
• In the first case, divideToIntegralValue(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 the non-fractional part of the result [this BigDecimal/ BigDecimal divsr]

Example:

```// Java program to demonstrate the example
// of divideToIntegralValue(BigDecimal divsr, MathContext    ma_co)
// method of BigDecimal

import java.math.*;
public class DivideToIntegralValueOfBD {
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(1, RoundingMode.CEILING);

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

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

System.out.println(" ");

System.out.println("divideToIntegralValue(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 integral part of the quotient
quotient = b_dec2.divideToIntegralValue(b_dec3, ma_co);
System.out.println("b_dec2.divideToIntegralValue(b_dec3,ma_co): " + quotient);
}
}
```

Output

```divideToIntegralValue(BigDecimal):
b_dec1.divideToIntegralValue(b_dec3): 24.00

divideToIntegralValue(BigDecimal,MathContext):
b_dec2.divideToIntegralValue(b_dec3,ma_co): 8
```
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.