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(BigDecimal num, int scale)
round
return num.divide(new BigDecimal("1"), scale, BigDecimal.ROUND_HALF_UP);
BigDecimalround(BigDecimal number)
Round to two decimal places
return number.setScale(2, RoundingMode.HALF_UP);
BigDecimalround(BigDecimal v, int scale, int roundingMode)
round
if (scale < 0 || roundingMode < 0) {
    throw new IllegalArgumentException("The scale or roundingMode must be a positive integer or zero");
return v.divide(BigDecimal.ONE, scale, roundingMode);
BigDecimalround(BigDecimal value)
round
return value.setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimalround(BigDecimal value)
round
if (value == null) {
    return ZERO_VALUE;
return round(value.doubleValue());
BigDecimalround(BigDecimal value, BigDecimal increment, RoundingMode roundingMode)
Util method for rounding number to an increment from wikipedia http://en.wikipedia.org/wiki/Rounding#Rounding_to_a_specified_increment In general, rounding a number x to a multiple of some specified increment m entails the following steps: Divide x by m, let the result be y; Round y to an integer value, call it q; Multiply q by m to obtain the rounded value z.
BigDecimal divided = value.divide(increment, 0, roundingMode);
BigDecimal result = divided.multiply(increment);
return result;
BigDecimalround(BigDecimal value, int accuracy)
This method is used for arbitrary accuracy rounding.
MathContext context = new MathContext(accuracy, RoundingMode.HALF_EVEN);
BigDecimal factor = BigDecimal.valueOf(10.0).pow(accuracy);
return value.multiply(factor).round(context).divide(factor);
BigDecimalround(BigDecimal value, int places)
round
if (places < 0)
    throw new IllegalArgumentException();
value = value.setScale(places, RoundingMode.HALF_UP);
return value;
BigDecimalround(BigDecimal value, String currency)
round
if (currency == null) {
    return value.setScale(2, BigDecimal.ROUND_HALF_UP);
} else {
    return value.setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimalround(BigDecimal what, int howmuch)
round
if (what == null) {
    return null;
double number = Double.parseDouble(what.toString());
double formatedNumber = (double) ((int) (number * Math.pow(10, howmuch) + .5)) / Math.pow(10, howmuch);
return BigDecimal.valueOf(formatedNumber);