Here you can find the source of roundToN(double d, int n)
Parameter | Description |
---|---|
d | - to be rounded |
n | - decimal places |
public static double roundToN(double d, int n)
//package com.java2s; //License from project: Apache License public class Main { /**//from ww w . j a va 2 s. c om * Rounds the d to n decimal places. Uses pow and round methods - do not bother to use in a loop, if required, * unfold the pow method. * * @param d * - to be rounded * @param n * - decimal places * @return - the rounded value */ public static double roundToN(double d, int n) { // may lose some precision due to fp errors double places = StrictMath.pow(10, n); double p = StrictMath.round(d * places) / places; return p; } }