Java BigDecimal

In this post, we will see about Java BigDecimal class.

BigDecimal class contains Arithmetic operations, rounding, comparison, scaling. It provides high precision arithmetic operations, so generally used to handle numbers in banking and financial domain.

Java BigDecimal provides a lot of options to control rounding. You can choose various rounding modes such as round half up or round up down to do the rounding.

Here is simple way to create a BigDecimal.We will use String based constructor for creating BigDecimal object.

An example to set scale upto 2 decimal places for BigDecimal:

As BigDecimal is immutable just like String,it always returns new object when you perform any operation on it. That’s why we need to assign b.setScale(2, BigDecimal.ROUND_HALF_UP) to b reference variable again.

Constructors

Constructor
BigDecimal(BigInteger val)
converts a BigInteger into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale)
converts a BigInteger unscaled value and an int scale into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)
converts a BigInteger unscaled value and an int scale into a BigDecimal, with rounding according to the context settings.
BigDecimal(BigInteger val, MathContext mc)
converts a BigInteger into a BigDecimal rounding according to the context settings.
BigDecimal(char[] in)
converts 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)
converts 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)
converts 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)
converts 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)
converts a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value.
BigDecimal(double val, MathContext mc)
converts a double into a BigDecimal, with rounding according to the context settings.
BigDecimal(int val)
converts an int into a BigDecimal.
BigDecimal(int val, MathContext mc)
converts an int into a BigDecimal, with rounding according to the context settings.
BigDecimal(long val)
converts a long into a BigDecimal.
BigDecimal(long val, MathContext mc)
converts a long into a BigDecimal, with rounding according to the context settings.
BigDecimal(String val)
converts the string representation of a BigDecimal into a BigDecimal.
BigDecimal(String val, MathContext mc)
converts 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.

Methods

Return TypeMethods
BigDecimalabs()
This method returns absolve value of BigDecimal. It will retain scale of the BigDecimal
BigDecimalabs ( MathContext  mc)
This methods Returns the absolute value of this BigDecimal, with rounding according to the MathContext settings.
BigDecimaladd ( BigDecimal  augend)
This method returns a BigDecimal whose value is sum of (this and augend), and whose scale is max(this.scale(), augend.scale()).
BigDecimaladd ( BigDecimal  augend,  MathContext  mc)
It returns a BigDecimal whose value is sum of (this and augend), with rounding according to the MathContext settings.
bytebyteValueExact()
It Converts this BigDecimal to a byte, checking for lost information.
intcompareTo ( BigDecimal  val)
This method is used to compare this to another BigDecimal.This method returns 0,1 or -1 based on input
BigDecimaldivide ( BigDecimal  divisor)
This method returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.
BigDecimaldivide ( BigDecimal  divisor, int roundingMode)
This method returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale() and it applies roundingMode based on the parameter.
BigDecimaldivide ( BigDecimal  divisor, int scale, int roundingMode)
This method returns a BigDecimal whose value is (this / divisor), and whose scale is as specified and it applies roundingMode based on the parameter.
BigDecimaldivide ( BigDecimal  divisor, int scale,  RoundingMode  roundingMode)
This method returns a  BigDecimal  whose value is  (this / divisor) , and whose scale is as specified.
BigDecimaldivide ( BigDecimal  divisor,  MathContext  mc)
This method returns a BigDecimal whose value is (this / divisor), with rounding according to the MathContext settings.
BigDecimaldivide ( BigDecimal  divisor,  RoundingMode  roundingMode)
This method returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().
BigDecimal[]divideAndRemainder ( BigDecimal  divisor)
This method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
BigDecimal[]divideAndRemainder ( BigDecimal  divisor,  MathContext  mc)
THis method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands calculated with rounding according to the MathContext settings.
BigDecimaldivideToIntegralValue ( BigDecimal  divisor)
This method returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.
BigDecimaldivideToIntegralValue ( BigDecimal  divisor,  MathContext  mc)
This method returns a BigDecimal whose value is the integer part of (this / divisor), with rounding according to MathContext setting
doubledoubleValue()
This method converts this BigDecimal to a double value.
booleanequals ( Object  x)
This method Compares this BigDecimal with the specified Object for equality. Please note that this method takes scale into account while applying equality.
floatfloatValue()
This method converts this BigDecimal to a float value.
inthashCode()
This method returns the hash code for this BigDecimal.
intintValue()
This method simply converts this BigDecimal to an int value.
intintValueExact()
This method converts this BigDecimal to an int, checking for lost information.
longlongValue()
This method converts this BigDecimal to a long value.
longlongValueExact()
This method converts this BigDecimal to a long, checking for lost information.
BigDecimalmax ( BigDecimal  val)
This method Returns the maximum of this BigDecimal and val passed.
BigDecimalmin ( BigDecimal  val)
This method returns the minimum of this BigDecimal and val passed.
BigDecimalmovePointLeft(int n)
This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.
BigDecimalmovePointRight(int n)
This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.
BigDecimalmultiply ( BigDecimal  multiplicand)
This method returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).
BigDecimalmultiply ( BigDecimal  multiplicand,  MathContext  mc)
This method returns a BigDecimal whose value is (this × multiplicand), with rounding according to the MathContext settings.
BigDecimalnegate()
This method returns a BigDecimal whose value is (-this), and whose scale is this.scale().
BigDecimalnegate ( MathContext  mc)
This method returns a BigDecimal whose value is (-this), with rounding according to the MathContext settings.
BigDecimalplus()
This method returns a BigDecimal whose value is (+this), and whose scale is this.scale().It is just opposite of negate().
BigDecimalplus ( MathContext  mc)
This method returns a BigDecimal whose value is (+this), with rounding according to the MathContext settings.
BigDecimalpow(int n)
This method returns a BigDecimal whose value is (this^n), The power is computed exactly, to unlimited precision.
BigDecimalpow (int n,  MathContext  mc)
This method Returns a BigDecimal whose value is (this^n), with rounding according to the MathContext settings.
intprecision()
This method simply returns the precision of this BigDecimal.
BigDecimalremainder ( BigDecimal  divisor)
This method returns a BigDecimal whose value is (this % divisor).
BigDecimalremainder ( BigDecimal  divisor,  MathContext  mc)
This method returns a BigDecimal whose value is (this % divisor), with rounding according to the MathContext settings.
BigDecimalround ( MathContext  mc)
This method returns a BigDecimal rounded according to the MathContext settings.
intscale()
This method returns the scale of this BigDecimal.
BigDecimalscaleByPowerOfTen(int n)
This method returns a BigDecimal whose numerical value is equal to (this * 10n).
BigDecimalsetScale(int newScale)
This method returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's.This is used very often to set scale of BigDecimal
BigDecimalsetScale(int newScale, int roundingMode)
This method returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
BigDecimalsetScale (int newScale,  RoundingMode  roundingMode)
This method returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
shortshortValueExact()
This method Converts this BigDecimal to a short, checking for lost information.
intsignum()
This method returns the signum function of this BigDecimal.
BigDecimalstripTrailingZeros()
This method returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.
BigDecimalsubtract ( BigDecimal  subtrahend)
This method returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).
BigDecimalsubtract ( BigDecimal  subtrahend,  MathContext  mc)
This method returns a BigDecimal whose value is (this - subtrahend), with rounding according to the MathContext settings.
BigIntegertoBigInteger()
This method converts this BigDecimal to a BigInteger.
BigIntegertoBigIntegerExact()
This method converts this BigDecimal to a BigInteger, checking for lost information.
StringtoEngineeringString()
This method returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.
StringtoPlainString()
This method returns a string representation of this BigDecimal without an exponent field.
StringtoString()
This method returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.
BigDecimalulp()
This method returns the size of an ulp, a unit in the last place, of this BigDecimal.
BigIntegerunscaledValue()
This method returns a BigInteger whose value is the unscaled value of this BigDecimal.
static BigDecimalvalueOf(double val)
This method translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method.
static BigDecimalvalueOf(long val)
This method translates a long value into a BigDecimal with a scale of zero.
static BigDecimalvalueOf(long unscaledVal, int scale)
This method translates a long unscaled value and an int scale into a BigDecimal.

Let’s see some examples with the help of FAQs on BigDecimal.

FAQs

How can you compare two BigDecimals?

You can compare two BigDecimals using compareTo method.

When you run above program, you will get below output:

First BigDecimal is greater than Second BigDecimal

How to set scale in case of BigDecimals

When you run above program, you will get below output:

BigDecimal Rounding to 3 decimal places: 11.275

How to convert String to BigDecimal?

You can simply use BigDecimal’s constructor to convert String to BigDecimal

When you run above program, you will get below output:

Converted String currency to bigDecimalCurrency: 23.12

How to convert BigDecimal to String?

You can simply use BigDecimal’s toString method to convert BigDecimal to String.

When you run above program, you will get below output:

12.92

When you run above program, you will get below output:

Converted String currency to bigDecimalCurrency: 23.12

How to add two BigDecimals in java?

You can use BigDecimal’s add method to divide two BigDecimals.

When you run above program, you will get below output:

12.6

How to subtract two BigDecimals in java?

You can use BigDecimal’s substract method to divide two BigDecimals.

When you run above program, you will get below output:

7.8

How to multiply two BigDecimals in java?

You can use BigDecimal’s multiply method to multiply two BigDecimals.

When you run above program, you will get below output:

24.48

How to divide two BigDecimals in java?

You can use BigDecimal’s divide method to divide two BigDecimals.

When you run above program, you will get below output:

4.25

How to remove trailing zero from BigDecimal?

You can use stripTrailingZeros to remove trailing zero from BigDecimal.

When you run above program, you will get below output:

10.2

That’s all about BigDecimal in java.

References:

https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *