Java BigDecimal Add sum(BigDecimal num1, BigDecimal num2, int scale)

Here you can find the source of sum(BigDecimal num1, BigDecimal num2, int scale)

Description

sum

License

Open Source License

Declaration

public static BigDecimal sum(BigDecimal num1, BigDecimal num2, int scale) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static BigDecimal sum(BigDecimal num1, BigDecimal num2, int scale) {
        return roundBigDecimal(num1.add(num2), scale);
    }/*from ww w .j a v a 2  s  .c  om*/

    public static BigDecimal sum(BigDecimal... amounts) {
        return sum(2, amounts);
    }

    public static BigDecimal sum(int scale, BigDecimal... amounts) {
        BigDecimal result = null;
        for (BigDecimal bigDecimal : amounts) {
            if (bigDecimal != null) {
                result = result == null ? bigDecimal : result.add(bigDecimal);
            }
        }
        return roundBigDecimal(result, scale);
    }

    public static BigDecimal roundBigDecimal(BigDecimal num) {
        return roundBigDecimal(num, 2);
    }

    public static BigDecimal roundBigDecimal(BigDecimal num, int scale) {
        if (num != null) {
            return num.setScale(scale, RoundingMode.HALF_UP);
        }
        return num;
    }
}

Related

  1. addToBigDecimalInMap(Map theMap, K mapKey, BigDecimal addNumber)
  2. addToBigDecimalInMap(Object key, BigDecimal value, Map theMap)
  3. addUlp(BigDecimal x)
  4. addVAT(int priceInCents, BigDecimal vat)
  5. addVatAmount(BigDecimal percentage, BigDecimal amount)
  6. sum(BigDecimal[] bigDecimalNumbers)
  7. sum(final List list)
  8. sum(List numbers)
  9. sumBig(final Collection values)