Java BigDecimal Round round(BigDecimal value, BigDecimal increment, RoundingMode roundingMode)

Here you can find the source of round(BigDecimal value, BigDecimal increment, RoundingMode roundingMode)

Description

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.

License

Open Source License

Parameter

Parameter Description
value - the value to round
increment -
roundingMode a parameter

Return

the rounded BigDecimal

Declaration

public static BigDecimal round(BigDecimal value, BigDecimal increment, RoundingMode roundingMode) 

Method Source Code


//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;
    }
}

Related

  1. round(BigDecimal num, int scale)
  2. round(BigDecimal number)
  3. round(BigDecimal v, int scale, int roundingMode)
  4. round(BigDecimal value)
  5. round(BigDecimal value)
  6. round(BigDecimal value, int accuracy)
  7. round(BigDecimal value, int places)
  8. round(BigDecimal value, String currency)
  9. round(BigDecimal what, int howmuch)