public static double roundToSignificantDigits(double value,int n){
  if (isEqual(value,0.0)) {
    return 0.0;
  }
  final double d=Math.ceil(Math.log10(value < 0.0 ? -value : value));
  final int power=n - (int)d;
  final double magnitude=Math.pow(10.0,power);
  final long shifted=Math.round(value * magnitude);
  return shifted / magnitude;
}

public static double roundToSignificantDigits ( double value , int n ) { if ( isEqual ( value , 0.0 ) ) { return 0.0 ; } final double d = Math . ceil ( Math . log10 ( value < 0.0 ? - value : value ) ) ; final int power = n - ( int ) d ; final double magnitude = Math . pow ( 10.0 , power ) ; final long shifted = Math . round ( value * magnitude ) ; return shifted / magnitude ; }
rounds a number to a given number of significant decimal digits . note that the number will be left with * only * this number of significant digits regardless of magnitude , e . g . 12345 to 3 digits will be 12300 , whereas 0 . 12345 will be 0 . 123 .
