Java Number Round roundToDecimals(double d, int c)

Here you can find the source of roundToDecimals(double d, int c)

Description

Returns a value with the desired number of decimal places.

License

Open Source License

Parameter

Parameter Description
d value to round
c number of decimal places desired. Must be greater or equal to zero, otherwise, the given value d would be returned without any modification.

Return

a value with the given number of decimal places.

Declaration

public final static double roundToDecimals(double d, int c) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

public class Main {
    /**//from  w w  w.j a v a 2 s.  c o m
     * Returns a value with the desired number of decimal places.
     * 
     * @param d
     *       value to round
     * @param c
     *       number of decimal places desired. 
     *       Must be greater or equal to zero, otherwise, the given value d would be returned without any modification.
     * @return
     *       a value with the given number of decimal places.
     */
    public final static double roundToDecimals(double d, int c) {
        if (c < 0)
            return d;
        double p = Math.pow(10, c);
        d = d * p;
        double tmp = Math.round(d);
        return tmp / p;
    }
}

Related

  1. roundToBytes(int bitWidth)
  2. roundToDay(long time)
  3. roundToDecentPrecision(double value)
  4. roundToDecimalPlaces(final double val, final int places)
  5. roundToDecimals(double d, int c)
  6. roundToDecimals(double d, int c)
  7. roundToDecimals(double d, int numberOfDecimalPlaces)
  8. roundToDecimals(double d, int percision)
  9. roundToDecimals(double doubleValue)