Jump to content
Killersites Community
• Sign Up

## Recommended Posts

anyone know how to multiply multiple BigDecimals?

not like this example:

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

I need to multiple more than two numbers and also do addition.

This is the line i'm trying to convert to BigDecimals:

`double aDeterminantMinuend = (a * a * a) + (a * a * a) + (a * a * a);`

as you can see those are doubles in an array that i'm doing my arithmetic to.

so I've prepped them like:

```BigDecimal a00 = new BigDecimal(a);
BigDecimal a01 = new BigDecimal(a);
BigDecimal a02 = new BigDecimal(a);

BigDecimal a10 = new BigDecimal(a);
BigDecimal a11 = new BigDecimal(a);
BigDecimal a12 = new BigDecimal(a);

BigDecimal a20 = new BigDecimal(a);
BigDecimal a21 = new BigDecimal(a);
BigDecimal a22 = new BigDecimal(a);```

so now I need to do the BigDecimal arithmetic to them, but the only examples i can find are like the one posted above. Just two BigDecimals multiplied together.

i've tried embedding multiple calls the multiple() method, but that doesnt work.

I'm thinking the only way to do it is to make new BigDecimals for each step of the arithmetic, but surely theres a better, neater way?

#### Share this post

##### Share on other sites

actually I think I've solved it:

```		BigDecimal a00 = new BigDecimal(a);
BigDecimal a01 = new BigDecimal(a);
BigDecimal a02 = new BigDecimal(a);

BigDecimal a10 = new BigDecimal(a);
BigDecimal a11 = new BigDecimal(a);
BigDecimal a12 = new BigDecimal(a);

BigDecimal a20 = new BigDecimal(a);
BigDecimal a21 = new BigDecimal(a);
BigDecimal a22 = new BigDecimal(a);

BigDecimal a00a11a22 = new BigDecimal("0.0");
BigDecimal a01a12a20 = new BigDecimal("0.0");
BigDecimal a02a10a21 = new BigDecimal("0.0");

BigDecimal a20a11a02 = new BigDecimal("0.0");
BigDecimal a21a12a00 = new BigDecimal("0.0");
BigDecimal a22a10a01 = new BigDecimal("0.0");

BigDecimal bigDecimalDeterminantMinuend = new BigDecimal("0.0");
BigDecimal bigDecimalDeterminantSubtrahend = new BigDecimal("0.0");

a00a11a22 = a00.multiply(a11).multiply(a22);
a01a12a20 = a01.multiply(a12).multiply(a20);
a02a10a21 = a02.multiply(a10).multiply(a21);

bigDecimalDeterminantMinuend = a00a11a22.add(a01a12a20).add(a02a10a21);

a20a11a02 = a20.multiply(a11).multiply(a02);
a21a12a00 = a21.multiply(a12).multiply(a00);
a22a10a01 = a22.multiply(a10).multiply(a01);

bigDecimalDeterminantSubtrahend = a20a11a02.add(a21a12a00).add(a22a10a01);```

Edited by DavidCampbell

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• Leaderboard

• Back
• Store