Here you can find the source of round(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode roundingMode)
public static BigDecimal round(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode roundingMode)
//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; } }