Java Double Number Round roundDecimals(double x, int decimals)

Here you can find the source of roundDecimals(double x, int decimals)

Description

Rounds a double to specified number of decimal places.

License

Open Source License

Parameter

Parameter Description
x The input number that needs to be rounded.
decimals The number of decimal places.

Return

The input number rounded to the number of decimal places specified.

Declaration

public static double roundDecimals(double x, int decimals) 

Method Source Code

//package com.java2s;
/* must be accompanied by the FIRST BSD license file in the root directory of */

public class Main {
    /**//from  w  w  w.  ja v  a  2 s  .c o m
     * Rounds a double to specified number of decimal places.
     * @param x The input number that needs to be rounded.
     * @param decimals The number of decimal places.
     * @return The input number rounded to the number of decimal places specified.
     */
    public static double roundDecimals(double x, int decimals) {
        if (decimals > 9)
            return (Math.ceil(x * decimals) / decimals);
        double z = 1;
        for (int i = 1; i <= decimals; i++)
            z *= 10;
        return (Math.ceil(x * z) / z);
    }
}

Related

  1. roundBig(double d)
  2. roundDec(float num, int dec)
  3. roundDecimal(Double value)
  4. roundDecimal(double x, int d)
  5. roundDecimals(double d, int decimalPlaces)
  6. roundDiv(double a, double b)
  7. roundDouble(double d)
  8. roundDouble(double d)
  9. roundDouble(double d, int decimals)