Java Double Number Round round(double value, int decimalPlaces)

Here you can find the source of round(double value, int decimalPlaces)

Description

Rounds the value with the specified number of decimal places.

License

Open Source License

Parameter

Parameter Description
value Value to round.
decimalPlaces Number of specified decimal places and have to be non negative.

Return

the value with the specified number of decimal places.

Declaration

public static double round(double value, int decimalPlaces) 

Method Source Code

//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&nbsp;*&nbsp;10^(decimal&nbsp;places))&nbsp;/&nbsp;10^(decimal&nbsp;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&nbsp;*&nbsp;10^(decimal&nbsp;places))&nbsp;/&nbsp;10^(decimal&nbsp;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;
        }
    }
}

Related

  1. round(double value, double precision)
  2. round(double value, int decimal)
  3. round(double value, int decimal)
  4. round(double value, int decimalPlace)
  5. round(double value, int decimalPlaces)
  6. round(double value, int decimals)
  7. round(double value, int decimals)
  8. round(double value, int exponent)
  9. round(double value, int nbDigits)