Java Number Round roundTo(double d, int n)

Here you can find the source of roundTo(double d, int n)

Description

returns the rounded result of the argument to the n digits after the decimal point.

License

Open Source License

Parameter

Parameter Description
d the double number to be rounded
n the number of digits after the decimal point

Declaration

public static double roundTo(double d, int n) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  . j a  v a 2  s.  co m*/
 This file is part of Coach Assistant
 Copyright (C) 2004-2006 Sina Iravanian  <sina_iravanian@yahoo.com>
    
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

public class Main {
    /**
     * returns the rounded result of the argument to the n digits after the
     * decimal point.
     * 
     * @param d
     *            the double number to be rounded
     * @param n
     *            the number of digits after the decimal point
     */
    public static double roundTo(double d, int n) {
        d *= Math.pow(10, n);
        d = Math.round(d);
        d /= Math.pow(10, n);
        return d;
    }
}

Related

  1. roundString(double n, double range, double precision)
  2. roundTime24h(final long defaultUnitValue)
  3. roundTimeStamp(long tstamp)
  4. roundTimeValue(final float defaultUnitValue, final boolean is24HourFormatting)
  5. roundTo(double aValue, int aDigits)
  6. roundTo(double num, int dp)
  7. roundTo(double val, double prec)
  8. roundTo(final double VALUE, final double TARGET)
  9. roundTo(float number, int base)