Here you can find the source of round(BigDecimal value, int accuracy)
Parameter | Description |
---|---|
value | is the value to be rounded. |
accuracy | is the accuracy of the requested rounding. A value greater 0 is the number of digits after the decimal sign. A value less than 0 rounds the number of digits before the decimal sign to zero. |
public static BigDecimal round(BigDecimal value, int accuracy)
//package com.java2s; //License from project: Open Source License import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; public class Main { /**//from ww w. j a v a 2 s.c o m * This method is used for arbitrary accuracy rounding. * * @param value * is the value to be rounded. * @param accuracy * is the accuracy of the requested rounding. A value greater 0 * is the number of digits after the decimal sign. A value less * than 0 rounds the number of digits before the decimal sign to * zero. * @return The rounded value is returned. */ public static double round(double value, int accuracy) { double factor = Math.pow(10.0, accuracy); return Math.round(value * factor) / factor; } /** * This method is used for arbitrary accuracy rounding. * * @param value * is the value to be rounded. * @param accuracy * is the accuracy of the requested rounding. A value greater 0 * is the number of digits after the decimal sign. A value less * than 0 rounds the number of digits before the decimal sign to * zero. * @return The rounded value is returned. */ public static BigDecimal round(BigDecimal value, int accuracy) { MathContext context = new MathContext(accuracy, RoundingMode.HALF_EVEN); BigDecimal factor = BigDecimal.valueOf(10.0).pow(accuracy); return value.multiply(factor).round(context).divide(factor); } }