Home »
Java »
Java Reference
Java BigDecimal Class: Methods, Constructors, Examples
By IncludeHelp Last updated : March 18, 2024
Java BigDecimal Class
In Java, the BigDecimal class is used for performing operations for arithmetic, comparison, hashing, rounding, manipulation and format conversion. The BigDecimal class provides various constructures and built-in methods to perform operations.
Java BigDecimal Class Declaration
public class BigDecimal
extends Number
implements Comparable<BigDecimal>
Java BigDecimal Class Constructors
Java BigDecimal class has the following built-in constructors:
Constructor |
Description |
BigDecimal(BigInteger val) |
Translates a BigInteger into a BigDecimal.
|
BigDecimal(BigInteger unscaledVal, int scale) |
translate a BigInteger unscaled value and an int scale into a BigDecimal.
|
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) |
Translates a BigInteger unscaled value and an int scale into a BigDecimal, with rounding according to the context settings.
|
BigDecimal(BigInteger val, MathContext mc) |
Translates a BigInteger into a BigDecimal rounding according to the context settings.
|
BigDecimal(char[ ] in) |
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor.
|
BigDecimal(char[ ] in, int offset, int len) |
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified.
|
BigDecimal(char[ ] in, int offset, int len, MathContext mc) |
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified and with rounding according to the context settings.
|
BigDecimal(char[ ] in, MathContext mc) |
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor and with rounding according to the context settings.
|
BigDecimal(double val) |
Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value.
|
BigDecimal(double val, MathContext mc) |
Translates a double into a BigDecimal, with rounding according to the context settings.
|
BigDecimal(int val) |
Translates an int into a BigDecimal.
|
BigDecimal(int val, MathContext mc) |
Translates an int into a BigDecimal, with rounding according to the context settings.
|
BigDecimal(long val) |
translate a long into a BigDecimal.
|
BigDecimal(long val, MathContext mc) |
Translates a BigInteger into a BigDecimal.
|
BigDecimal(String val) |
the string representation of a BigDecimal into a BigDecimal.
|
BigDecimal(String val, MathContext mc) |
Translates the string representation of a BigDecimal into a BigDecimal, accepting the same strings as the BigDecimal(String) constructor, with rounding according to the context settings.
|
Java BigDecimal Class Methods
Java BigDecimal class has the following built-in methods:
Method |
Description |
abs() |
It is used to get a BigDecimal that holds the absolute value of this BigDecimal. |
add() |
It is used to get a BigDecimal that holds the value added this BigDecimal with the given BigDecimal and its scale is calculated by using max([this BigDecimal.scale()] , [BigDecimal val.scale()]). |
byteValueExact() |
It is used to convert a BigDecimal to exact byte value. |
compareTo() |
It is used to compare this BigDecimal object to the given object. |
divideAndRemainder() |
It 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 remainder on the two values of "BigDecimal" type manipulated. |
divideToIntegralValue() |
It 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()). |
doubleValue() |
It is used to convert a BigDecimal to a double value. |
equals() |
It is used to check whether this BigDecimal and the given object are equal or not. |
floatValue() |
It is used to convert a BigDecimal to a float value. |
hashCode() |
It is used to get the hash code value for this BigDecimal object. |
intValue() |
It is used to convert a BigDecimal to an integer. |
intValueExact() |
It is used to convert this BigDecimal into an exact integer value. |
longValue() |
It is used to convert this BigDecimal to a long value. |
longValueExact() |
It is used to convert this BigDecimal to an exact long value. |
max() |
It is used to get the greatest value of (this BigDecimal) and the given (BigDecimal ob). |
min() |
It is used to get the least value of (this BigDecimal) and the given (BigDecimal ob). |
movePointLeft() |
It is used to get a BigDecimal which is similar to this BigDecimal with the decimal point shifted to the given number of places to the left side. |
movePointRight() |
It is used to get a BigDecimal which is similar to this BigDecimal with the decimal point shifted to the given number of places to the right side. |
multiply() |
It is used get a BigDecimal that holds the value multiplied this BigDecimal by the given BigDecimal and its scale is calculated by using ([this BigDecimal.scale()] * [BigDecimal m_val.scale()]). |
negate() |
It is used to get a negation of this BigDecimal and its scale is non-negative. |
plus() |
It is used to get the value with (+) representation of this BigDecimal. |
pow() |
It is used to calculate the value of this BigDecimal is raised to the power of the given parameter (x) (i.e. [(this BigDecimal) pow (x)]). |
precision() |
It is used to get the precision of this BigDecimal object and we all know the term precision is the number of digits represented in the un-scaled value. |
remainder() |
It is used to calculate the remainder by using ([this BigDecimal] % divsr). |
round() |
It is used to get a rounded BigDecimal based on the given MathContext setting when the precision value is not equal to 0 otherwise there is no effect of rounding when precision value is equal to 0. |
scale() |
It is used to get the scale of this BigDecimal object. |
setScale() |
It is used to set the new scale with the given scale of this BigDecimal object. |
scaleByPowerOfTen() |
It is used to get a BigDecimal whose value is calculated by using [(this BigDecimal) * 10 pow(number)]. |
shortValueExact() |
It is used to convert this BigDecimal to an exact short value. |
signum() |
It is used to get the signum (sign number) function of this BigDecimal object. |
stripTrailingZeros() |
It is used to get a BigDecimal that is equivalent to this BigDecimal but without any Trailing zeros. |
subtract() |
It is used to get a BigDecimal that holds the value subtracted the given parameter (val) from this BigDecimal. |
toBigInteger() |
It is used to convert this BigDecimal object value into a BigInteger object value and the decimal part will be skipped during conversion of this BigDecimal. |
toBigIntegerExact() |
It is used to convert this BigDecimal into the exact BigInteger value and it will throw an exception when this BigDecimal holds some fractional part that is other than 0. |
toEngineeringString() |
It is used to represent string denotation of this BigDecimal with the help of engineering notation when an exponent is required to denote BigDecimal as a String. |
toPlainString() |
It is used to represent string denotation of this BigDecimal without the help of any notation and no exponent is required to denote BigDecimal as a String. |
toString() |
It is used to represent string denotation of this BigDecimal with the help of scientific notation when an exponent field is required to denote BigDecimal as String. |
ulp() |
It is used to get the size of an ulp of this BigDecimal. |
unscaledValue() |
It is used to calculate the unscaled value by using the formula ([this BigDecimal] * 10 pow [this BigDecimal.scale()] ) of this BigDecimal as a BigInteger. |
valueOf() |
It is used to convert the given double value into a BigDecimal. |
Java BigDecimal Examples
Example 1
Below example demonstrates the use of popular methods of BigDecimal class in Java:
// Importing BigDecimal Class
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
// Create two new BigDecimals
BigDecimal objBD1 =
new BigDecimal("1234567891.1981983918");
BigDecimal objBD2 =
new BigDecimal("9998888111.7777171717");
// add() Method
objBD1 = objBD1.add(objBD2);
System.out.println("Result : " + objBD1);
// multiply() Method
objBD1 = objBD1.multiply(objBD2);
System.out.println("Result : " + objBD1);
// subtract() Method
objBD1 = objBD1.subtract(objBD2);
System.out.println("Result : " + objBD1);
// divide() Method
objBD1 = objBD1.divide(objBD2);
System.out.println("Result : " + objBD1);
// pow() Method
objBD1 = objBD1.pow(2);
System.out.println("Result : " + objBD1);
// negate() Method
objBD1 = objBD1.negate();
System.out.println("Result : " + objBD1);
}
}
The output of the above program is:
Result : 11233456002.9759155635
Result : 112322069682333914378.27083138376323175295
Result : 112322069672335026266.49311421206323175295
Result : 11233456001.9759155635
Result : 126190533748328721088.48915431408152253225
Result : -126190533748328721088.48915431408152253225
Example 2
Below example finds the maximum and minimum numbers from two BigDecimal values in Java:
// Importing BigDecimal Class
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
//Creating BigDecimal objects
BigDecimal objBD1, objBD2;
objBD1 = new BigDecimal("12345");
objBD2 = new BigDecimal("98765");
// Getting max and min values
BigDecimal bdMax = objBD1.max(objBD2);
BigDecimal bdMin = objBD1.min(objBD2);
// Printing
System.out.println("Max Value : " + bdMax);
System.out.println("Min Value : " + bdMin);
}
}
The output of the above program is:
Max Value : 98765
Min Value : 12345
Implemented Interfaces
- Serializable
- Comparable<BigDecimal>
Reference