Java Double Number Round round(double d)

Here you can find the source of round(double d)

Description

Rounds the number to 4 significant digits

License

Open Source License

Declaration

public static double round(double d) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** Rounds the number to 4 significant digits */
    public static double round(double d) {
        if (d == 0.0)
            return d;
        int digits = (int) (Math.log(d) / Math.log(10));
        return round(d, 3 - digits);
    }//from   w  ww.j  a  v  a2s .  c om

    /** Rounds the number to n decimal places */
    public static double round(double d, int n) {
        double shift = Math.pow(10, n);
        return ((double) ((long) (d * shift + 0.5))) / shift;
    }
}

Related

  1. round(double d)
  2. round(double d)
  3. round(double d)
  4. round(double d)
  5. round(double d)
  6. round(double d)
  7. round(double d, int decimalPlacesRequired)
  8. round(double d, int decimals)
  9. round(double d, int i)