Big Decimal in Java

Pasan Kamburugamuwa
3 min readOct 5, 2021

One of the solutions to a floating-point error in java is Big decimal. We can define the BigDecimal as an arbitrary-precision signed immutable number. This BigDecimal has two parts namely,

1. Unscaled value — an arbitrary precision integer.

2. Scale — A 32-bit integer representing the number of digits to the right of the decimal point.

So as an example, we get the number 5.432, this has an unscaled value of 5432, and the scale value is 3.

BigDecimal is really helpful when you dealing with high-precision arithmetic operations. In below, I have added a sample code which is using addition, subtraction, multiplication and division.

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {
bigDecimal();
}

public static void bigDecimal(){

//two new Big Decimals
BigDecimal b1 = new BigDecimal("56789123.567892132");
BigDecimal b2 = new BigDecimal("12342323.123422323");


//add two decimals
BigDecimal addTwoDecimal = b1.add(b2);
System.out.println("Add two decimal " + addTwoDecimal);

//substract two decimals
BigDecimal substractTwoDecimal = b1.add(b2);
System.out.println("Substract two decimal " + substractTwoDecimal);

//multiply two decimals
BigDecimal multiplyTwoDecimal = b1.multiply(b2);
System.out.println("Multiply two decimal " + multiplyTwoDecimal);

//divide two decimals
//When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED),
// arithmetic operations are exact, as are the arithmetic methods which take no MathContext object.
BigDecimal divideTwoDecimal = b1.divide(b2, 2, RoundingMode.HALF_UP);
System.out.println("Divide two decimal " + divideTwoDecimal);

// BigDecima1 raised to the power of 2
BigDecimal rasideTothePowerDecimalTwo = b1.pow(6);
System.out.println("Raise to the power of 2 = " + rasideTothePowerDecimalTwo);

// Negative value of BigDecimal1
BigDecimal negativeValue= b1.negate();
System.out.println("Negative value = " + negativeValue);

List<BigDecimal> myList = new ArrayList<>();

myList.add(BigDecimal.valueOf(56789123.567892132));
myList.add(BigDecimal.valueOf(12342323.123422323));

System.out.println(myList);


}
}

Let’s get a real-world example. Try to run the below application on your machine.

public static void main(String[] args) {
for(double i = 10; i != 0; i = i - 0.1){
System.out.println(i);
}
}

This will endlessly as below.

19999977905
-34459.29999977905
-34459.399999779045
-34459.499999779044
-34459.59999977904
-34459.69999977904
-34459.79999977904
-34459.89999977904
-34459.99999977904
-34460.099999779035
-34460.19999977903
-34460.29999977903
-34460.39999977903
-34460.49999977903
-34460.59999977903
-34460.699999779026
-34460.799999779025
-34460.89999977902
-34460.99999977902
-34461.09999977902
-34461.19999977902
...
...

To avoid this floating point error, as we discussed, we can use BigDecimal. Below, is the code that is used to avoid the floating-point error in java using BigDecimal.

public class Main {
public static void main(String[] args) {
for(BigDecimal i = new BigDecimal("10");
i.compareTo(new BigDecimal("0")) != 0;
i = i.subtract(new BigDecimal("0.1"))
){
System.out.println(i);
}
}
}

Now the output will be as follow with exact values.

10
9.9
9.8
9.7
9.6
9.5
9.4
9.3
9.2
9.1
9.0
8.9
8.8
8.7
8.6
8.5
8.4
8.3
8.2
8.1
8.0
7.9
7.8
7.7
7.6
7.5
7.4
7.3
7.2
7.1
7.0
6.9
6.8
6.7
6.6
6.5
6.4
6.3
6.2
6.1
6.0
5.9
5.8
5.7
5.6
5.5
5.4
5.3
5.2
5.1
5.0
4.9
4.8
4.7
4.6
4.5
4.4
4.3
4.2
4.1
4.0
3.9
3.8
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0
2.9
2.8
2.7
2.6
2.5
2.4
2.3
2.2
2.1
2.0
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1

So this is all about how to solve the floating-point error using BigDecimals in java. Hope you enjoy the tutorial and see you in next tutorial.

--

--