Here you can find the source of roundSig(double d, int n)
public static double roundSig(double d, int n)
//package com.java2s; //License from project: Apache License public class Main { public static double roundSig(double d, int n) { if (d != d) { return Double.NaN; }/* w w w. j av a2 s. com*/ if (d == 0) { return 0; } int power = n - (int) Math.ceil(Math.log10(d < 0 ? -d : d)); double magnitude = Math.pow(10, power); return Math.round(d * magnitude) / magnitude; } public static double round(double d, int decimals) { if (d != d) { return Double.NaN; } double n = Math.pow(10, decimals); return Math.round(d * n) / n; } public static double round(double d, double fraction) { if (d != d) { return Double.NaN; } return Math.round(d * fraction) / fraction; } }