Here you can find the source of 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.
Parameter | Description |
---|---|
value | - the value to round |
increment | - |
roundingMode | a parameter |
public static BigDecimal round(BigDecimal value, BigDecimal increment, RoundingMode roundingMode)
//package com.java2s; import java.math.BigDecimal; import java.math.RoundingMode; public class Main { /**//from ww w . j av a2 s. co m * Util method for rounding number to an increment * from wikipedia {@link 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. * @param value - the value to round * @param increment - * @param roundingMode * @return the rounded BigDecimal */ public static BigDecimal round(BigDecimal value, BigDecimal increment, RoundingMode roundingMode) { //please see method documentation //Divide x by m, let the result be y //Round y to an integer value, call it q; BigDecimal divided = value.divide(increment, 0, roundingMode); //Multiply q by m to obtain the rounded value z. BigDecimal result = divided.multiply(increment); return result; } }