Java Utililty Methods BigDecimal Calculate

List of utility methods to do BigDecimal Calculate

Description

The list of methods to do BigDecimal Calculate are organized into topic(s).

Method

BigDecimalcalculateMonthCapital(BigDecimal eachIssueMoney, BigDecimal eachMonthInterest)
calculate Month Capital
return eachIssueMoney.subtract(eachMonthInterest);
BigDecimalcalculateMonthInterest(Float interestRate, BigDecimal investmoney)
calculate Month Interest
return investmoney.multiply(new BigDecimal(Float.toString(interestRate)))
        .multiply(BigDecimal.ONE.divide(new BigDecimal(12), 10, RoundingMode.HALF_UP));
BigDecimalcalculatePercentChange(BigDecimal from, BigDecimal to)
calculate Percent Change
BigDecimal difference = to.subtract(from);
return difference.divide(from, 8, RoundingMode.HALF_UP).multiply(CENT);
BigDecimalcalculateProfit(BigDecimal totalAmt, Integer duration, BigDecimal rate)
calculate Profit
Calendar cal = Calendar.getInstance();
int maxDayNum = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
return totalAmt.multiply(new BigDecimal(duration)).multiply(rate).divide(new BigDecimal(maxDayNum * 100), 2,
        RoundingMode.CEILING);
BigDecimalcalculateRatioInDecimal(BigDecimal numberA, BigDecimal numberB)
berechnet das prozentuelle verhaeltnis von 2 Zahlen
BigDecimal result = null;
if (numberA.compareTo(numberB) < 0) {
    result = new BigDecimal(1).subtract(numberA.divide(numberB, BigDecimal.ROUND_HALF_EVEN));
} else if (numberA.compareTo(numberB) == 0) {
    result = new BigDecimal(0);
} else {
    result = new BigDecimal(1).subtract(numberB.divide(numberA, BigDecimal.ROUND_HALF_EVEN));
return result.multiply(new BigDecimal(100.0));
BigDecimalcoerce(BigDecimal val, int targetPrecision, int targetScale)
Attempts to coerce a big decimal to a target precision and scale and returns the result.
if (val.scale() != targetScale) {
    try {
        val = val.setScale(targetScale, RoundingMode.UNNECESSARY);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException(
                "Value scale " + val.scale() + " can't be coerced to target scale " + targetScale + ". ");
if (val.precision() > targetPrecision) {
    throw new IllegalArgumentException("Value precision " + val.precision()
            + " (after scale coercion) can't be coerced to target precision " + targetPrecision + ". ");
return val;
BigDecimalcosh(final BigDecimal dec, final int scale, final RoundingMode mode)

This method calculates the cosh using a series expansion.
Sourced from wolfram

This method uses the #factorial(int) method for demoninators, and #power(BigDecimal,long) for exponents.

This expansion ceases execution when the next component in the series is equal to zero for the given scale.

BigDecimal value = BigDecimal.ONE; 
int nextIndex = 1;
do {
    final int exponent = nextIndex++ << 1;
    final BigDecimal entry = power(dec, exponent) 
            .divide(new BigDecimal(factorial(exponent)), scale, mode); 
    if (entry.unscaledValue().equals(BigInteger.ZERO))
        break; 
...
BigDecimalcosine(BigDecimal x)
cosine
BigDecimal currentValue = BigDecimal.ONE;
BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
BigDecimal xSquared = x.multiply(x);
BigDecimal numerator = BigDecimal.ONE;
BigDecimal denominator = BigDecimal.ONE;
int i = 0;
while (lastVal.compareTo(currentValue) != 0) {
    lastVal = currentValue;
...