Here you can find the source of round(double d, int p)
Parameter | Description |
---|---|
d | the double to round |
p | the number of decimal places |
public static double round(double d, int p)
//package com.java2s; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; public class Main { /**/*from w w w. j a v a 2 s. c om*/ * Round a double to <b>p</b> decimal places * @param d the double to round * @param p the number of decimal places * @return the double with <b>p</b> decimal places */ public static double round(double d, int p) { String format = "0."; for (int i = 0; i < p; i++) format += "0"; DecimalFormat twoDForm = new DecimalFormat(format, new DecimalFormatSymbols(Locale.US)); return Double.valueOf(twoDForm.format(d)); } }