Here you can find the source of roundTo(double d, int n)
Parameter | Description |
---|---|
d | the double number to be rounded |
n | the number of digits after the decimal point |
public static double roundTo(double d, int n)
//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; } }