Java Decimal Round round(double d, int p)

Here you can find the source of round(double d, int p)

Description

Round a double to p decimal places

License

Open Source License

Parameter

Parameter Description
d the double to round
p the number of decimal places

Return

the double with p decimal places

Declaration

public static double round(double d, int p) 

Method Source Code


//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));
    }
}

Related

  1. getRoundedValue(double doubleValue)
  2. getRoundedValue(double value)
  3. round(double d, int n)
  4. round(double dValue)
  5. round(double number)
  6. round(double unrounded, int precision)
  7. round(final double value)