Q:

Calculation library

I need to do some calculations like this (simplified):
            amount1     amount2     amount3     amount4     total
            5000        500         1000        350         6850

3.55%       177.5       17.75       35.5        12.43       243.18
1.00%       50          5           10          3.5         68.5

-           4772.5      477.25      954.5       334.07      6538.32

16.66%      ....
...

The calculations are pretty easy; But I'm not sure how I best implement this.
I don't only need to be able to fetch and show the total results from the calculations but also every sub calculation.
I can make a method for every sub calculation and group methods together in one for a total calculation. 
f.e.:
calcTotal1() {
    calcSub1() + calcSub2() + ... ;
}

But I was wondering if there is a better way to implement this.
It also should be a little library that can be used by different programs.

A:

I think, your requirement could be nicely met with an array of percentages to be calculated:
 public BigDecimal[] calculate(BigDecimal[] percentages, BigDecimal amount) {
     BigDecimal rolling = amount;
     BigDecimal[] result = new BigDecimal[percentages.length];
     for (int i = 0; i < percentages.length; i++) {
        // We use null as a marker for subtotals
        if (percentages[i] != null) {
           result[i] = amount.multiply(percentages[i]).divide(new BigDecimal("100"), MathContext.DECIMAL128);
           rolling = rolling.add(result[i]);
        } else {
           // Insert the subTotal
           result[i] = rolling;
        }
     }

Please note the use of BigDecimals, while they do use a bit more memory, it will have a severe impact on the correctness of your figures.
With nulls in the percentages[], you can specify rows where subtotals have to be printed out.

