List of utility methods to do BigDecimal Round
BigDecimal | round(BigDecimal num, int scale) round return num.divide(new BigDecimal("1"), scale, BigDecimal.ROUND_HALF_UP); |
BigDecimal | round(BigDecimal number) Round to two decimal places return number.setScale(2, RoundingMode.HALF_UP);
|
BigDecimal | round(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); |
BigDecimal | round(BigDecimal value) round return value.setScale(2, BigDecimal.ROUND_HALF_UP);
|
BigDecimal | round(BigDecimal value) round if (value == null) { return ZERO_VALUE; return round(value.doubleValue()); |
BigDecimal | round(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;
|
BigDecimal | round(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); |
BigDecimal | round(BigDecimal value, int places) round if (places < 0) throw new IllegalArgumentException(); value = value.setScale(places, RoundingMode.HALF_UP); return value; |
BigDecimal | round(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); |
BigDecimal | round(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); |