Here you can find the source of roundToDecimals(double d, int c)
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. |
public final static double roundToDecimals(double d, int c)
//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; } }