Java Double Number Round round(double val)

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

Description

round

License

Open Source License

Declaration

public static double round(double val) 

Method Source Code

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

public class Main {
    public static double round(double val) {
        int precision = 10000; //keep 4 digits
        return Math.floor(val * precision + .5) / precision;
    }//from w ww.  ja v a  2  s .com

    public static double round(float val) {
        int precision = 10000; //keep 4 digits
        return Math.floor(val * precision + .5) / precision;
    }

    public static double round(double val, int n) {
        int precision = 1;
        for (int i = 0; i < n; i++)
            precision *= 10;
        return Math.floor(val * precision + .5) / precision;
    }

    public static float round(float val, int n) {
        int precision = 1;
        for (int i = 0; i < n; i++)
            precision *= 10;
        return (float) (Math.floor(val * precision + .5) / precision);
    }
}

Related

  1. round(double toRound)
  2. round(double v)
  3. round(double v, int precision)
  4. round(double val)
  5. round(double val)
  6. round(double val, int decimalHouses)
  7. round(double val, int decimalPlaces)
  8. round(double val, int places)
  9. round(double val, int places)