Here you can find the source of round(BigDecimal b, int precision)
round a number to an arbitrary precision based on http://www.crazysquirrel.com/computing/java/basics/rounding.jspx
public static BigDecimal round(BigDecimal b, int precision)
//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); } }