Java Double Number Round roundDouble(double value, int afterDecimalPoint)

Here you can find the source of roundDouble(double value, int afterDecimalPoint)

Description

Rounds a double to the given number of decimal places.

License

Open Source License

Parameter

Parameter Description
value the double value
afterDecimalPoint the number of digits after the decimal point

Return

the double rounded to the given precision

Declaration

public static  double roundDouble(double value, int afterDecimalPoint) 

Method Source Code

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

public class Main {
    /**/*from ww  w .ja v  a 2 s  .c  o  m*/
     * Rounds a double to the given number of decimal places.
     *
     * @param value the double value
     * @param afterDecimalPoint the number of digits after the decimal point
     * @return the double rounded to the given precision
     */
    public static /*@pure@*/ double roundDouble(double value, int afterDecimalPoint) {

        double mask = Math.pow(10.0, (double) afterDecimalPoint);

        return (double) (Math.round(value * mask)) / mask;
    }

    /**
     * Rounds a double to the next nearest integer value. The JDK version
     * of it doesn't work properly.
     *
     * @param value the double value
     * @return the resulting integer value
     */
    public static /*@pure@*/ int round(double value) {

        int roundedValue = value > 0 ? (int) (value + 0.5) : -(int) (Math.abs(value) + 0.5);

        return roundedValue;
    }
}

Related

  1. roundDouble(double num, int precision)
  2. roundDouble(double number, int precision)
  3. roundDouble(double pDouble, int pPlaces)
  4. roundDouble(double val, int precision)
  5. roundDouble(double value)
  6. roundDouble3(double r)
  7. roundDoubleDownTo(double value, double steps)
  8. roundDoubleNicely(double intensity)
  9. roundDoubleTo(double val, int pow)