Java Double Number Round round(double[] array, int decimals)

Here you can find the source of round(double[] array, int decimals)

Description

Rounds a number to the nearest double given the number of required decimal places.

License

Open Source License

Parameter

Parameter Description
array Array to be rounded
decimals Decimal places

Return

Rounded number

Declaration

public static double[] round(double[] array, int decimals) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors://from  www .ja v  a2s.  c om
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

public class Main {
    /**
     * Rounds a number to the closest {@code double} given the number of
     * required decimal places.
     *
     * @param number Number to be rounded
     * @param decimals Decimal places
     * @return Rounded number
     * @since 0.2.2
     */
    public static double round(double number, int decimals) {
        double out = number * Math.pow(10, decimals);
        out = Math.round(out) / Math.pow(10, decimals);

        return out;
    }

    /**
     * Rounds a number to the nearest {@code double} given the number of
     * required decimal places.
     *
     * @param array Array to be rounded
     * @param decimals Decimal places
     * @return Rounded number
     */
    public static double[] round(double[] array, int decimals) {
        double[] out = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            out[i] = round(array[i], decimals);
        }

        return out;
    }
}

Related

  1. round(double x)
  2. round(double x, int numPlaces)
  3. round(double x, int places)
  4. round(double zahl, int stellen)
  5. round(double[] arr)
  6. round(double[][] data)
  7. round(final double num)
  8. round(final double num, final int prec)
  9. round(final double numberToRound, final int decimalPlaces)