Java BigDecimal Round round(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode roundingMode)

Here you can find the source of round(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode roundingMode)

Description

round

License

Open Source License

Declaration

public static BigDecimal round(final BigDecimal number, final int minFractionDigits,
            final int maxFractionDigits, final RoundingMode roundingMode) 

Method Source Code


//package com.java2s;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.MessageFormat;

public class Main {
    public static float round(final float number, final int maxFractionDigits, final RoundingMode roundingMode) {
        // Validating input.
        if (roundingMode == null) {
            throw new NullPointerException("roundingMode");
        }/* w w w .j  a va2s .co m*/
        if (maxFractionDigits < 0) {
            throw new IllegalArgumentException(
                    MessageFormat.format("Negative maxFractionDigits: {0}.", String.valueOf(maxFractionDigits)));
        }

        return roundImpl(new BigDecimal(Float.toString(number)), 0, maxFractionDigits, roundingMode).floatValue();
    }

    public static double round(final double number, final int maxFractionDigits, final RoundingMode roundingMode) {
        // Validating input.
        if (roundingMode == null) {
            throw new NullPointerException("roundingMode");
        }
        if (maxFractionDigits < 0) {
            throw new IllegalArgumentException(
                    MessageFormat.format("Negative maxFractionDigits: {0}.", String.valueOf(maxFractionDigits)));
        }

        return roundImpl(BigDecimal.valueOf(number), 0, maxFractionDigits, roundingMode).doubleValue();
    }

    public static BigDecimal round(final BigDecimal number, final int minFractionDigits,
            final int maxFractionDigits, final RoundingMode roundingMode) {
        // Validating input.
        if (number == null) {
            throw new NullPointerException("number");
        }
        if (roundingMode == null) {
            throw new NullPointerException("roundingMode");
        }
        if (minFractionDigits < 0) {
            throw new IllegalArgumentException(
                    MessageFormat.format("Negative minFractionDigits: {0}.", String.valueOf(minFractionDigits)));
        }
        if (maxFractionDigits < 0) {
            throw new IllegalArgumentException(
                    MessageFormat.format("Negative maxFractionDigits: {0}.", String.valueOf(maxFractionDigits)));
        }
        if (minFractionDigits > maxFractionDigits) {
            throw new IllegalArgumentException(
                    MessageFormat.format("minFractionDigits ({0}) is greater than maxFractionDigits ({1}).",
                            String.valueOf(minFractionDigits), String.valueOf(maxFractionDigits)));
        }

        return roundImpl(number, minFractionDigits, maxFractionDigits, roundingMode);
    }

    private static BigDecimal roundImpl(final BigDecimal number, final int minFractionDigits,
            final int maxFractionDigits, final RoundingMode mode) {
        final int scale = number.scale();
        if (scale < minFractionDigits) {
            return number.setScale(minFractionDigits, mode);
        }
        if (scale > maxFractionDigits) {
            return number.setScale(maxFractionDigits, mode);
        }
        return number;
    }
}

Related

  1. round(BigDecimal value, int accuracy)
  2. round(BigDecimal value, int places)
  3. round(BigDecimal value, String currency)
  4. round(BigDecimal what, int howmuch)
  5. round(final BigDecimal input, final int precision)
  6. round_BigDecimal(double d, int decLen)
  7. roundBigDecimal(BigDecimal num)
  8. roundBigDecimal(Number value, double precision)
  9. roundCommercial(BigDecimal value, int decimalPlaces)