Java BigDecimal Round getRoundedAmt(BigDecimal amtBeforeRnd)

Here you can find the source of getRoundedAmt(BigDecimal amtBeforeRnd)

Description

Round the amount to the nearest 5 cents
For example:
0.12 ---> 0.10
0.13 ---> 0.15
0.17 ---> 0.15
0.18 ---> 0.20

License

Apache License

Parameter

Parameter Description
notRoundAmt Amount before rounded

Return

Amount rounded to the nearest 5 cents

Declaration

public static BigDecimal getRoundedAmt(BigDecimal amtBeforeRnd) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {
    private static final BigDecimal increment = new BigDecimal("0.05");

    /**//from  w w w. j  a  v  a 2s  . c  o  m
     * Round the amount to the nearest 5 cents<br>
     * For example:<br>
     * 0.12 ---> 0.10<br>
     * 0.13 ---> 0.15<br>
     * 0.17 ---> 0.15<br>
     * 0.18 ---> 0.20
     * 
     * @param notRoundAmt
     *            Amount before rounded
     * @return Amount rounded to the nearest 5 cents
     */
    public static BigDecimal getRoundedAmt(BigDecimal amtBeforeRnd) {
        BigDecimal divided = amtBeforeRnd.divide(increment, 0, BigDecimal.ROUND_HALF_UP);
        BigDecimal roundedAmt = divided.multiply(increment);
        return roundedAmt;
    }
}

Related

  1. average(int precision, RoundingMode roundingMode, BigDecimal... decimals)
  2. decimalRound(BigDecimal number, int numDigits, RoundingMode rounding)
  3. getEfficientRound(BigDecimal val, int effectiveDigit)
  4. getRoundedBigDecimal(double value)
  5. getRoundedBigDecimal(double value, int scale)
  6. getStringValue(BigDecimal val, int newScale, int roundingMode)
  7. magicRound(BigDecimal value)