Java BigDecimal

Previous
Next

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 Type Methods
BigDecimal abs()
This method returns absolve value of BigDecimal. It will retain scale of the BigDecimal
BigDecimal abs ( MathContext  mc)
This methods Returns the absolute value of this BigDecimal, with rounding according to the MathContext settings.
BigDecimal add ( BigDecimal  augend)
This method returns a BigDecimal whose value is sum of (this and augend), and whose scale is max(this.scale(), augend.scale()).
BigDecimal add ( BigDecimal  augend,  MathContext  mc)
It returns a BigDecimal whose value is sum of (this and augend), with rounding according to the MathContext settings.
byte byteValueExact()
It Converts this BigDecimal to a byte, checking for lost information.
int compareTo ( BigDecimal  val)
This method is used to compare this to another BigDecimal.This method returns 0,1 or -1 based on input
BigDecimal divide ( 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.
BigDecimal divide ( 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.
BigDecimal divide ( 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.
BigDecimal divide ( BigDecimal  divisor, int scale,  RoundingMode  roundingMode)
This method returns a  BigDecimal  whose value is  (this / divisor) , and whose scale is as specified.
BigDecimal divide ( BigDecimal  divisor,  MathContext  mc)
This method returns a BigDecimal whose value is (this / divisor), with rounding according to the MathContext settings.
BigDecimal divide ( 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.
BigDecimal divideToIntegralValue ( BigDecimal  divisor)
This method returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.
BigDecimal divideToIntegralValue ( BigDecimal  divisor,  MathContext  mc)
This method returns a BigDecimal whose value is the integer part of (this / divisor), with rounding according to MathContext setting
double doubleValue()
This method converts this BigDecimal to a double value.
boolean equals ( 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.
float floatValue()
This method converts this BigDecimal to a float value.
int hashCode()
This method returns the hash code for this BigDecimal.
int intValue()
This method simply converts this BigDecimal to an int value.
int intValueExact()
This method converts this BigDecimal to an int, checking for lost information.
long longValue()
This method converts this BigDecimal to a long value.
long longValueExact()
This method converts this BigDecimal to a long, checking for lost information.
BigDecimal max ( BigDecimal  val)
This method Returns the maximum of this BigDecimal and val passed.
BigDecimal min ( BigDecimal  val)
This method returns the minimum of this BigDecimal and val passed.
BigDecimal movePointLeft(int n)
This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.
BigDecimal movePointRight(int n)
This method returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.
BigDecimal multiply ( BigDecimal  multiplicand)
This method returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).
BigDecimal multiply ( BigDecimal  multiplicand,  MathContext  mc)
This method returns a BigDecimal whose value is (this × multiplicand), with rounding according to the MathContext settings.
BigDecimal negate()
This method returns a BigDecimal whose value is (-this), and whose scale is this.scale().
BigDecimal negate ( MathContext  mc)
This method returns a BigDecimal whose value is (-this), with rounding according to the MathContext settings.
BigDecimal plus()
This method returns a BigDecimal whose value is (+this), and whose scale is this.scale().It is just opposite of negate().
BigDecimal plus ( MathContext  mc)
This method returns a BigDecimal whose value is (+this), with rounding according to the MathContext settings.
BigDecimal pow(int n)
This method returns a BigDecimal whose value is (this^n), The power is computed exactly, to unlimited precision.
BigDecimal pow (int n,  MathContext  mc)
This method Returns a BigDecimal whose value is (this^n), with rounding according to the MathContext settings.
int precision()
This method simply returns the precision of this BigDecimal.
BigDecimal remainder ( BigDecimal  divisor)
This method returns a BigDecimal whose value is (this % divisor).
BigDecimal remainder ( BigDecimal  divisor,  MathContext  mc)
This method returns a BigDecimal whose value is (this % divisor), with rounding according to the MathContext settings.
BigDecimal round ( MathContext  mc)
This method returns a BigDecimal rounded according to the MathContext settings.
int scale()
This method returns the scale of this BigDecimal.
BigDecimal scaleByPowerOfTen(int n)
This method returns a BigDecimal whose numerical value is equal to (this * 10n).
BigDecimal setScale(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
BigDecimal setScale(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.
BigDecimal setScale (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.
short shortValueExact()
This method Converts this BigDecimal to a short, checking for lost information.
int signum()
This method returns the signum function of this BigDecimal.
BigDecimal stripTrailingZeros()
This method returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.
BigDecimal subtract ( BigDecimal  subtrahend)
This method returns a BigDecimal whose value is (this – subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).
BigDecimal subtract ( BigDecimal  subtrahend,  MathContext  mc)
This method returns a BigDecimal whose value is (this – subtrahend), with rounding according to the MathContext settings.
BigInteger toBigInteger()
This method converts this BigDecimal to a BigInteger.
BigInteger toBigIntegerExact()
This method converts this BigDecimal to a BigInteger, checking for lost information.
String toEngineeringString()
This method returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.
String toPlainString()
This method returns a string representation of this BigDecimal without an exponent field.
String toString()
This method returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.
BigDecimal ulp()
This method returns the size of an ulp, a unit in the last place, of this BigDecimal.
BigInteger unscaledValue()
This method returns a BigInteger whose value is the unscaled value of this BigDecimal.
static BigDecimal valueOf(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 BigDecimal valueOf(long val)
This method translates a long value into a BigDecimal with a scale of zero.
static BigDecimal valueOf(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

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog