Method that rounds a value to the n precision decimals - Java java.lang

Java examples for java.lang:double

Description

Method that rounds a value to the n precision decimals

Demo Code

/*// ww w  .  j a v a 2s.co m
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * 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
 */
//package com.java2s;

public class Main {
    /**
     * Method that rounds a value to the n precision decimals
     * @param value
     * @param precision
     * @return a double value rounded to n precision
     */
    public static double roundDouble(double value, int precision) {
        int rounder = (int) Math.pow(10, precision);
        return (double) Math.round(value * rounder) / rounder;
    }
}

Related Tutorials