Java Number Round roundSig(double d, int n)

Here you can find the source of roundSig(double d, int n)

Description

round Sig

License

Apache License

Declaration

public static double roundSig(double d, int n) 

Method Source Code

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

Related

  1. roundRobin(final int value)
  2. roundScale(double value, int scale)
  3. roundScale2(Number number)
  4. roundSec(int sec)
  5. roundSec(long milli)
  6. roundSignificant(double d, int numDigit, boolean keepInteger)
  7. roundSignificant(double value)
  8. roundSimpleNumberUnits(final long graphDefaultUnit)
  9. roundSizes(float[] sizes)