How to Do BigDecimal Arithmetic for Java

By Doug Lowe

The worst part about using the BigDecimal class in Java is that you can’t use normal arithmetic operators with BigDecimal objects. BigDecimal arithmetic has different rules.T he following code, for example, won’t compile:

BigDecimal subTotal, taxRate, tax, total;
subTotal = new BigDecimal("32.50");
taxRate = new BigDecimal("0.05");
tax = subTotal * taxRate; // error: won’t compile
total = subTotal + tax  // this won’t compile either

Instead, you have to call methods of the BigDecimal class to perform basic arithmetic. All these methods return the result of the calculation as BigDecimal objects. Here’s how you can perform the preceding tax calculation:

BigDecimal subTotal, taxRate, tax, total;
subTotal = new BigDecimal("32.50");
taxRate = new BigDecimal("0.05");
tax = subTotal.multiply(taxRate);
total = subTotal.add(tax);

Here is a list of all the arithmetic methods for the BigDecimal class. As you can see, the class has methods for basic operations such as add, subtract, multiply, and divide, as well as some additional operations such as abs (absolute power), pow (raising the number to a power), and negative (changes the sign of the number).

Method Explanation
BigDecimal abs() Returns the absolute value of this BigDecimal.
BigDecimal add Adds the specified BigDecimal to this
BigDecimal and returns the result.
BigDecimal divide(BigDecimal
val)
Divides this BigDecimal by the
specified BigDecimal and returns the
result. The method may throw ArithmeticException.
BigDecimal[]divideAndRemainder(BigDecimal
val)
Divides this BigDecimal by the
specified BigDecimal. The result and the
remainder are returned as a two-element BigDecimal array.
BigDecimal[]divideToIntegralValue(BigDecimal
val)
Divides this BigDecimal by the
specified BigDecimal and returns the
integer result.
BigDecimal max(BigDecimal val) Returns the larger of this BigDecimal
and the specified BigDecimal.
BigDecimal min(BigDecimal val) Returns the smaller of this BigDecimal and the specified BigDecimal.
BigDecimal multiply(BigDecimal
val)
Multiplies this BigDecimal by the
specified BigDecimal and returns the
result.
BigDecimal negate() Negates this BigDecimal and returns
the result.
BigDecimal pow(int power) Raises this BigDecimal to the power
specified by the int value and returns
the result.
BigDecimal remainder(BigDecimal
val)
Divides this BigDecimal by the
specified BigDecimal and returns the
remainder.
BigDecimal subtract(BigDecimal
val)
Subtracts the specified BigDecimal
from this BigDecimal and returns the
result.

Notice that in addition to normal division, you can use the divideToIntegralValue method to return the integer part of the result. Here’s an example:

BigDecimal a = new BigDecimal("23.5");
BigDecimal b = new BigDecimal("7.0");
BigDecimal c;
c = a.divideToIntegralValue(b);

After these statements execute, the value of c is 3.

You can also use the remainder method to get the remainder from a division:

c = a.remainder(b);

Here the value of c is 2.5.

A common mistake among new users of the BigDecimal class is forgetting to assign the result of an arithmetic operation. You might stare at this code loop for hours, wondering why it doesn’t seem to work:

BigDecimal totalWinnings = new BigDecimal("1000.00");
BigDecimal winningsThisGame = new BigDecimal("200.00");
totalWinnings.add(winningsThisGame);
System.out.println(totalWinnings);

The third statement adds winningsThisGame to totalWinnings but discards the result. What you probably meant was this:

totalWinnings = totalWinnings.add(winningsThisGame);