Here you can find the source of round(double value, int places)
Parameter | Description |
---|---|
value | a parameter |
places | a parameter |
public static double round(double value, int places)
//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); } }