How to use BigDecimal in Java by Examples

Tags: Java BigDecimal

As a Java developer if you are working with financial systems or developing systems that need granularity in your calculation then you should thinking about BigDecimal. BigDecimal is an immutable class, provides in java.math package. It consists of operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. In this blog post we will learn how to use BigDecimal through a list of examples.

How to initialize BigDecimal object

We can initialize BigDecimal object by using constructor or static factory methods:

BigDecimal bigDecimal1 = new BigDecimal(123456.123);
BigDecimal bigDecimal2 = new BigDecimal(1234567890L);
BigDecimal bigDecimal3 = new BigDecimal("123456.123");
BigDecimal bigDecimal4 = new BigDecimal("123456.123");
BigDecimal bigDecimal5 = BigDecimal.valueOf(1234567890L);
BigDecimal bigDecimal6 = BigDecimal.valueOf(123456.123);
BigDecimal bigDecimal7 = BigDecimal.valueOf(1000, 3);

How to do addition with BigDecimal

BigDecimal bigDecimal1 = new BigDecimal("10.001");
BigDecimal bigDecimal2 = new BigDecimal(10.002);

BigDecimal result = bigDecimal1.add(bigDecimal2);

How to do subtraction with BigDecimal

BigDecimal bigDecimal1 = new BigDecimal("20.02");
BigDecimal bigDecimal2 = new BigDecimal(10.01);

BigDecimal result = bigDecimal1.subtract(bigDecimal2);

How to do multiplication with BigDecimal

BigDecimal bigDecimal1 = new BigDecimal("30.01");
BigDecimal bigDecimal2 = new BigDecimal(2);

BigDecimal result = bigDecimal1.multiply(bigDecimal2);

How to do division with BigDecimal

BigDecimal bigDecimal1 = new BigDecimal("40.02");
BigDecimal bigDecimal2 = new BigDecimal(2);

BigDecimal result = bigDecimal1.divide(bigDecimal2);

How to compare BigDecimal values

We can use compareTo or equals methods to do comparision for BigDecimal value.

BigDecimal bigDecimal1 = new BigDecimal("10.01");
BigDecimal bigDecimal2 = new BigDecimal("10.02");
BigDecimal bigDecimal3 = new BigDecimal("10.02");

int result = bigDecimal1.compareTo(bigDecimal2);
boolean isEqual1 = bigDecimal1.equals(bigDecimal3);
boolean isEqual2 = bigDecimal2.equals(bigDecimal3);

How to convert BigDecimal to other types

BigDecimal provides methods to primitive data type as double, float, byte, short, int and long.

BigDecimal bigDecimal1 = new BigDecimal("10.01");

double doubleValue = bigDecimal1.doubleValue();
float floatValue = bigDecimal1.floatValue();
int intValue = bigDecimal1.intValue();
long longValue = bigDecimal1.longValue();
short shortValue = bigDecimal1.shortValue();
byte byteValue = bigDecimal1.byteValue();

How to round BigDecimal value

BigDecimal bigDecimal1 = new BigDecimal("4.5678");
BigDecimal result = bigDecimal1.round(new MathContext(3));

Some other useful methods

Calculate exponent value

BigDecimal bigDecimal1 = new BigDecimal("10");
BigDecimal powResult = bigDecimal1.pow(2);
To get negative value of a BigDecimal object
BigDecimal bigDecimal1 = new BigDecimal("10");
BigDecimal negativeResult = bigDecimal1.negate();

Download Source Code

The full source code in this blog can be found at: https://github.com/simplesolutiondev/BigDecimalExamples

Happy Coding!