Constructing BigDecimal Numbers – Selected API Classes

Constructing BigDecimal Numbers

Selected constructors of the BigDecimal class are shown below.

BigDecimal(int value)
BigDecimal(long value)
BigDecimal(double value)

Create a BigDecimal with the decimal number representation of the specified value. Note that these constructors can result in a loss of precision if the specified numerical value does not have an exact representation.

Click here to view code image

BigDecimal(String strValue)

Creates a BigDecimal from the string representation of a numerical value. Recommended as the preferred way of creating BigDecimal numbers.

The following valueOf() methods also create BigDecimal numbers:

Click here to view code image

static BigDecimal valueOf(long value)
static BigDecimal valueOf(double value)

Create a BigDecimal from the specified numerical value. The second method uses the string representation of the double value obtained from the Double.toString(double) method to create a BigDecimal.

The following code creates BigDecimal values and also shows the string representation of the BigDecimal numbers created. It shows that the constructor BigDecimal(String) or the static valueOf() methods are preferable when creating BigDecimal values, especially for decimal values that cannot be represented as exact double values.

Click here to view code image

BigDecimal dTobd   = new BigDecimal(0.7);        // 0.6999999999999999555910…
BigDecimal strTobd = new BigDecimal(“0.7”);      // 0.7
BigDecimal valTobd = BigDecimal.valueOf(0.7);    // 0.7

Computing with BigDecimal Numbers

Selected methods that perform common arithmetic operations with BigDecimal numbers are given below.

The limitations of arithmetic operators on decimal values are shown by the code below. An incorrect result may be computed if the representation of a decimal value is inexact in the format of the double primitive type.

Click here to view code image

double d1 = 0.70;
double d2 = 0.10;
System.out.println(d1 + d2);

Output:
 0.7999999999999999

However, using BigDecimal values gives the correct result:

Click here to view code image

BigDecimal bd1b = new BigDecimal(“0.70”);
BigDecimal bd2b = new BigDecimal(“0.10”);
System.out.println(bd1b.add(bd2b));
Output:
 0.80

Finally, here is a simple arithmetic calculation using BigDecimal numbers to compute the total cost of 103 items, priced at $2.99 each and 25% sales tax.

Click here to view code image

BigDecimal price     = new BigDecimal(“2.99”);
BigDecimal tax       = new BigDecimal(“0.25”);
BigDecimal quantity  = BigDecimal.TEN.pow(3);
BigDecimal totalCost = price.add(price.multiply(tax)).multiply(quantity);
System.out.println(totalCost);

Output:
 3737.5000

It is worth keeping in mind that BigDecimal numbers are immutable, and not to fall into the pitfall shown in the code below:

Click here to view code image

BigDecimal sum = BigDecimal.ZERO;
sum.add(BigDecimal.ONE);           // sum is not updated! Returns new BigDecimal.

Selected methods for computing with BigDecimal numbers:

Click here to view code image

BigDecimal add(BigDecimal val)
BigDecimal subtract(BigDecimal val)
BigDecimal multiply(BigDecimal val)
BigDecimal divide(BigDecimal val)
BigDecimal remainder(BigDecimal val)

Return a BigDecimal whose value represents the result of performing the operation (this + val), (this – val), (this * val), (this / val), or (this % val), respectively.

BigDecimal abs()

Returns a BigDecimal whose value is the absolute value of this BigDecimal.

BigDecimal negate()

Returns a BigDecimal whose value is (-this).

BigDecimal pow(int n)

Returns a BigDecimal whose value is (thisn).

Leave a Reply

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