Here you can find the source of round(double n, double nd)
public static final double round(double n, double nd)
//package com.java2s; //License from project: Apache License public class Main { public static final double round(double n, double nd) { if (isFinite(n) && isFinite(nd)) { double sign_n = (n < 0) ? -1 : 1; double abs_n = Math.abs(n); double factor = Math.pow(10, nd); return sign_n * Math.round(abs_n * factor) / factor; } else {/*from w w w . j av a 2 s.co m*/ return Double.NaN; } } public static final boolean isFinite(double x) { return !(Double.isInfinite(x) || Double.isNaN(x)); } }