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

BigDecimalround(final BigDecimal input, final int precision)
round
final MathContext mc = new MathContext(precision + 1);
return input.round(mc);
BigDecimalround(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode roundingMode)
round
if (number == null) {
    throw new NullPointerException("number");
if (roundingMode == null) {
    throw new NullPointerException("roundingMode");
if (minFractionDigits < 0) {
    throw new IllegalArgumentException(
...
doubleround_BigDecimal(double d, int decLen)
roun Big Decimal
BigDecimal bigD = new BigDecimal(d);
if (decLen < 0) {
    double tempValue = bigD.movePointLeft(-decLen).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
    bigD = (new BigDecimal(tempValue)).movePointRight(-decLen);
} else {
    bigD = bigD.setScale(decLen, BigDecimal.ROUND_HALF_UP);
return bigD.doubleValue();
...
BigDecimalroundBigDecimal(BigDecimal num)
round Big Decimal
return roundBigDecimal(num, 2);
NumberroundBigDecimal(Number value, double precision)
rounding with big decimal precision
if (value != null) {
    BigDecimal val = value instanceof BigDecimal ? (BigDecimal) value : new BigDecimal(value.toString());
    BigDecimal prec = BigDecimal.valueOf(precision);
    return val.divide(prec, BigDecimal.ROUND_HALF_EVEN).setScale(0, BigDecimal.ROUND_HALF_EVEN)
            .multiply(prec);
return null;
BigDecimalroundCommercial(BigDecimal value, int decimalPlaces)
round Commercial
return value.round(getMathContext(decimalPlaces));
StringroundDecimal(Object value)
round Decimal
return roundDecimal(value, "###,###.##");
FloatroundDecimals(Float d)
Round a float to decimal
Float roundedFloat = null;
if (d != null) {
    int nrdec = getNumberOfDecimalPlace(d);
    StringBuilder strbuf = new StringBuilder();
    strbuf.append("#.");
    for (int i = 0; i < nrdec; i++) {
        strbuf.append("#");
    DecimalFormat decformatter = decFormatMapCache.get(strbuf.toString());
    if (decformatter == null) {
        decformatter = new DecimalFormat(strbuf.toString(), DECIMALSEP);
        decFormatMapCache.put(strbuf.toString(), decformatter);
    roundedFloat = Float.valueOf(decformatter.format(d));
return roundedFloat;
BigDecimalroundDoubleAsBigDecimal(double d, int numberOfDecimals)
Rounds a double with the given number of decimals and returns a BigDecimal.
BigDecimal bigDecimal = new BigDecimal(d).setScale(numberOfDecimals, BigDecimal.ROUND_HALF_UP);
return bigDecimal;
introundDown(BigDecimal a)
round Down
if (isZero(a)) {
    return 0;
int i = a.intValue();
return i;