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

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

Description

Round the given double value to the specified number of places after the decimal point.

License

Open Source License

Parameter

Parameter Description
value a parameter
places a parameter

Declaration

public static double round(double value, int places) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Karim Ali and Ond?ej Lhot?k.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w w  . ja  v a2  s.  com*/
 *     Karim Ali - initial API and implementation and/or initial documentation
 *******************************************************************************/

public class Main {
    /**
     * Round the given double value to the specified number of places after the decimal point.
     * 
     * @param value
     * @param places
     * @return
     */
    public static double round(double value, int places) {
        if (places < 0)
            throw new IllegalArgumentException();

        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = Math.round(value);
        return (double) tmp / factor;
    }

    /**
     * Round a double value to the nearest two decimal places.
     * 
     * @param value
     * @return
     */
    public static double round(double value) {
        return round(value, 2);
    }
}

Related

  1. round(double value, int exponent)
  2. round(double value, int nbDigits)
  3. round(double value, int numberOfDecimalPlaces)
  4. round(double value, int numDecimals)
  5. round(double value, int places)
  6. round(double value, int power)
  7. round(double value, int precision)
  8. round(double value, int precision)
  9. round(double value, int precision)