Here you can find the source of round(double value, int decimalPlaces)
Parameter | Description |
---|---|
value | Value to round. |
decimalPlaces | Number of specified decimal places and have to be non negative. |
public static double round(double value, int decimalPlaces)
//package com.java2s; /*/*from ww w.ja va2s . co m*/ * This file is part of Bukkit Plugin Utilities. * * Bukkit Plugin Utilities is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * Bukkit Plugin Utilities 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Bukkit Plugin Utilities. * If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Rounds the value with the specified number of decimal places. Basically a * <code>Math.Round(value * 10^(decimal places)) / 10^(decimal places)</code> * . * * @param value * Value to round. * @param decimalPlaces * Number of specified decimal places and have to be non * negative. * @return the value with the specified number of decimal places. * @since 1.3 * @see Math#round(double) */ public static double round(double value, int decimalPlaces) { if (decimalPlaces < 0) { throw new IllegalArgumentException("Decimal places has to be non negative."); } else if (decimalPlaces == 0) { return Math.round(value); } else { final double factor = Math.pow(10, decimalPlaces); return Math.round(value * factor) / factor; } } /** * Rounds the value with the specified number of decimal places. Basically a * <code>Round(value * 10^(decimal places)) / 10^(decimal places)</code> * . * * @param value * Value to round. * @param decimalPlaces * Number of specified decimal places and have to be non * negative. * @return the value with the specified number of decimal places. * @since 1.3 * @see Math#round(float) */ public static float round(float value, int decimalPlaces) { if (decimalPlaces < 0) { throw new IllegalArgumentException("Decimal places has to be non negative."); } if (decimalPlaces == 0) { return Math.round(value); } else { final float factor = (float) Math.pow(10, decimalPlaces); return Math.round(value * factor) / factor; } } }