Here you can find the source of roundDecimals(double d, int numOfDec)
public static float roundDecimals(double d, int numOfDec)
//package com.java2s; //License from project: Apache License import java.text.DecimalFormat; public class Main { public static float roundDecimals(double d, int numOfDec) { if (numOfDec <= 0) return Math.round(d); String decFormatStr = "#."; for (int i = 0; i < numOfDec; i++) decFormatStr += "#"; DecimalFormat decFormat = new DecimalFormat(decFormatStr); return Float.valueOf(decFormat.format(d)).floatValue(); }/*from w w w . j a v a 2s .co m*/ }