Java BigDecimal Round round(BigDecimal b, int precision)

Here you can find the source of round(BigDecimal b, int precision)

Description

round a number to an arbitrary precision based on http://www.crazysquirrel.com/computing/java/basics/rounding.jspx

License

Open Source License

Declaration



public static BigDecimal round(BigDecimal b, int precision) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class Main {
    /**/*  w w w . ja  v a 2 s  .c o m*/
     * round a number to an arbitrary precision based on
     * http://www.crazysquirrel.com/computing/java/basics/rounding.jspx
     * */
    /*
     * public static Double round(Double d, int precision) { //BigDecimal b=new
     * BigDecimal(d); //MathContext context = new MathContext( ( b.precision() -
     * b.scale() + precision ), RoundingMode.HALF_UP); //return
     * b.round(context).doubleValue(); double exp=Math.pow(10, precision);
     * d*=exp; long dlong=Math.round(d);
     * 
     * return dlong/exp; }
     */

    public static BigDecimal round(BigDecimal b, int precision) {
        // BigDecimal b=new BigDecimal(d);
        MathContext context = new MathContext((b.precision() - b.scale() + precision), RoundingMode.HALF_UP);
        return b.round(context);
    }
}

Related

  1. getRoundedBigDecimal(double value, int scale)
  2. getStringValue(BigDecimal val, int newScale, int roundingMode)
  3. magicRound(BigDecimal value)
  4. round(BigDecimal amount)
  5. round(BigDecimal aValue, int aScale)
  6. round(BigDecimal d, int decimalDigits, RoundingMode rmode)
  7. round(BigDecimal d, int decimalPlace)
  8. round(BigDecimal decimal)
  9. round(BigDecimal decimal)