Java Double Number Round roundDouble(double num, int precision)

Here you can find the source of roundDouble(double num, int precision)

Description

round Double

License

Open Source License

Declaration

public static double roundDouble(double num, int precision) 

Method Source Code

//package com.java2s;

public class Main {
    public static double roundDouble(double num, int precision) {
        return ((double) Math.round(num * pow(10, precision))) / (pow(10, precision));

    }//from   w ww  .j  av  a 2s  .c om

    public static double pow(double base, int exp) {
        return exp == 0 ? 1 : sq(pow(base, exp / 2)) * (exp % 2 == 1 ? base : 1);
    }

    public static long pow(long base, int exp) {
        return exp == 0 ? 1 : sq(pow(base, exp / 2)) * (exp % 2 == 1 ? base : 1);
    }

    public static int pow(int base, int exp) {
        return exp == 0 ? 1 : sq(pow(base, exp / 2)) * (exp % 2 == 1 ? base : 1);
    }

    private static double sq(double x) {
        return x * x;
    }

    private static long sq(long x) {
        return x * x;
    }

    private static int sq(int x) {
        return x * x;
    }
}

Related

  1. roundDouble(double d)
  2. roundDouble(double d)
  3. roundDouble(double d, int decimals)
  4. roundDouble(double d, int place)
  5. roundDouble(double d, int places)
  6. roundDouble(double number, int precision)
  7. roundDouble(double pDouble, int pPlaces)
  8. roundDouble(double val, int precision)
  9. roundDouble(double value)