Java Number Round roundToSignificantFigures(double num, int n)

Here you can find the source of roundToSignificantFigures(double num, int n)

Description

round To Significant Figures

License

LGPL

Declaration

public static double roundToSignificantFigures(double num, int n) 

Method Source Code

//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;
    }
}

Related

  1. roundToQuarterMs(float f)
  2. roundToShort(final double d)
  3. roundToSignificantDigits(double num, int n)
  4. roundToSignificantDigits(double value, int n)
  5. roundToSignificantDigits(double x, double y, int nSignif)
  6. RoundToStr(double value, int dec)
  7. roundToString(double nb2round, int nbOfDigits)
  8. roundToTop(float f)
  9. roundToWord(long value)