Java Utililty Methods BigDecimal Round

List of utility methods to do BigDecimal Round

Description

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

Method

BigDecimalaverage(int precision, RoundingMode roundingMode, BigDecimal... decimals)
Returns the average or mean from the given list of decimal numbers.
BigDecimal result = null;
if (decimals != null) {
    int length = 0;
    for (BigDecimal decimal : decimals) {
        if (decimal != null) {
            if (result == null) {
                result = decimal;
            } else {
...
BigDecimaldecimalRound(BigDecimal number, int numDigits, RoundingMode rounding)
Rounding for decimals with support for negative digits
BigDecimal rounded = number.setScale(numDigits, rounding);
if (numDigits < 0) {
    rounded = rounded.setScale(0, BigDecimal.ROUND_UNNECESSARY);
return rounded;
StringgetEfficientRound(BigDecimal val, int effectiveDigit)
The numerical value below a significant figure is rounded off.
if (effectiveDigit <= 0) {
    return val.toString();
int intLength = getIntLength(val);
int delta = intLength - effectiveDigit;
if (delta > 0) {
    BigDecimal tenPower = new BigDecimal(new BigInteger("10").pow(delta));
    BigDecimal unscaledValue = new BigDecimal(val.unscaledValue());
...
BigDecimalgetRoundedAmt(BigDecimal amtBeforeRnd)
Round the amount to the nearest 5 cents
For example:
0.12 ---> 0.10
0.13 ---> 0.15
0.17 ---> 0.15
0.18 ---> 0.20
BigDecimal divided = amtBeforeRnd.divide(increment, 0, BigDecimal.ROUND_HALF_UP);
BigDecimal roundedAmt = divided.multiply(increment);
return roundedAmt;
BigDecimalgetRoundedBigDecimal(double value)
Get a big decimal rounded out to NUMBER_DECIMAL_PLACES places from the supplied double.
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(NUMBER_DECIMAL_PLACES, RoundingMode.HALF_UP);
return bd;
BigDecimalgetRoundedBigDecimal(double value, int scale)
get Rounded Big Decimal
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(scale, RoundingMode.HALF_UP);
return bd;
StringgetStringValue(BigDecimal val, int newScale, int roundingMode)
get String Value
if (val.compareTo(new BigDecimal(val.intValue())) == 0) {
    val = val.setScale(0);
} else {
    val = val.setScale(newScale, roundingMode);
return "" + val;
BigDecimalmagicRound(BigDecimal value)
magic Round
if (value.compareTo(MINUS_ONE) > 0 && value.compareTo(ONE) < 0) {
    return value.setScale(2 - (int) Math.log10(Math.abs(value.doubleValue())), BigDecimal.ROUND_HALF_UP);
} else {
    return value;
BigDecimalround(BigDecimal amount)
round
if (amount != null) {
    return amount.setScale(2, RoundingMode.HALF_UP);
} else {
    return null;
BigDecimalround(BigDecimal aValue, int aScale)
round
if (aValue == null)
    return null;
aValue = aValue.setScale(aScale, BigDecimal.ROUND_HALF_UP);
return aValue;