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

BigDecimalrounded(BigDecimal amount)
Rounds an amount to two decimal places.
return amount.setScale(DECIMALS, ROUNDING_MODE);
DoubleroundHalfUp2Scale(BigDecimal value)
round Half Up Scale
if (value == null)
    return null;
BigDecimal bd = value.setScale(2, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
BigDecimalroundImpl(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode mode)
round Impl
final int scale = number.scale();
if (scale < minFractionDigits) {
    return number.setScale(minFractionDigits, mode);
if (scale > maxFractionDigits) {
    return number.setScale(maxFractionDigits, mode);
return number;
...
BigDecimalroundingError(final BigDecimal dividend, final BigDecimal divisor, final int roundingMode)
Calculates the rounding error when dividend is divided by divisor
return dividend.subtract(dividend.divide(divisor, divisor.scale(), roundingMode).multiply(divisor));
BigDecimalroundOff(final BigDecimal amount)
This method is a utility method, which takes a BigDecimal and rounds that to 0 places.
return amount.setScale(DECIMALS, ROUNDING_MODE);
BigDecimalroundPrice(BigDecimal price, BigDecimal ticksize)
Rounds the specified price to the specified tick size
return roundPrice(price, ticksize, ticksize.scale());
BigDecimalroundTo(final BigDecimal bd, final int numberOfDecPlaces, final int finalScale)
returns a new BigDecimal with correct scale after being round to n dec places.
return setScale(setScale(bd, numberOfDecPlaces, BigDecimal.ROUND_HALF_UP), finalScale);
BigDecimalroundToMSDecimal(BigDecimal sourceDecimal)
round To MS Decimal
BigInteger sourceDecimalIntComponent = sourceDecimal.unscaledValue();
BigDecimal destinationDecimal = new BigDecimal(sourceDecimalIntComponent, sourceDecimal.scale());
byte roundingModel = 4;
validateDecimalMinMax(destinationDecimal);
BigInteger allWordBigInt = destinationDecimal.unscaledValue();
if (allWordBigInt.bitLength() > 96) {
    destinationDecimal = destinationDecimal.round(new MathContext(29));
    if (allWordBigInt.bitLength() > 96) {
...
BigDecimalroundUp1(BigDecimal number)
round Up
return number.setScale(0, RoundingMode.UP);
BigDecimalvalueOfBigDecimalRounding(double d1, int decimalPrecision)
value Of Big Decimal Rounding
BigDecimal bd = new BigDecimal(d1);
MathContext mc = new MathContext(decimalPrecision, RoundingMode.HALF_UP);
return bd.round(mc);