Here you can find the source of toIntArray(double[] array)
Parameter | Description |
---|---|
array | Input array |
public static int[] toIntArray(double[] array)
//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; } }