Java Integer Create toIntArray(double[] array)

Here you can find the source of toIntArray(double[] array)

Description

Converts a double array to an int array

License

Open Source License

Parameter

Parameter Description
array Input array

Return

int array

Declaration

public static int[] toIntArray(double[] array) 

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:/* ww w  .ja  v  a 2 s  . c  o m*/
 *     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 {
    /**
     * Converts a {@code double} array to an {@code int} array
     *
     * @param array Input array
     * @return {@code int} array
     */
    public static int[] toIntArray(double[] array) {
        int[] out = new int[array.length];
        for (int i = 0; i < out.length; i++)
            out[i] = (int) Math.round(array[i]);

        return out;
    }

    /**
     * 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. toIntArray(byte[] data)
  2. toIntArray(byte[] data, boolean includeLength)
  3. toIntArray(byte[] data, int offset)
  4. toIntArray(byte[] input)
  5. toIntArray(double[] a)
  6. toIntArray(final byte[] array)
  7. toIntArray(final byte[] bytes)
  8. toIntArray(final double[] doubleArray)
  9. toIntArray(final int ip)