Here you can find the source of roundToSignificantFigures(double num, int n)
public static double roundToSignificantFigures(double num, int n)
//package com.java2s; //License from project: LGPL public class Main { public static double roundToSignificantFigures(double num, int n) { if (num == 0) { return 0; }/* w ww. ja v a 2s .c o m*/ final double d = Math.ceil(Math.log10(num < 0 ? -num : num)); final int power = n - (int) d; final double magnitude = Math.pow(10, power); final long shifted = Math.round(num * magnitude); return shifted / magnitude; } }