Here you can find the source of truncateDigits(double input, int numberDigits)
public static String truncateDigits(double input, int numberDigits)
//package com.java2s; /**************************************************** Statistics Online Computational Resource (SOCR) http://www.StatisticsResource.org//from w w w. j a v a 2s. c o m All SOCR programs, materials, tools and resources are developed by and freely disseminated to the entire community. Users may revise, extend, redistribute, modify under the terms of the Lesser GNU General Public License as published by the Open Source Initiative http://opensource.org/licenses/. All efforts should be made to develop and distribute factually correct, useful, portable and extensible resource all available in all digital formats for free over the Internet. SOCR resources are distributed in the hope that they will be useful, but without any warranty; without any explicit, implicit or implied warranty for merchantability or fitness for a particular purpose. See the GNU Lesser General Public License for more details see http://opensource.org/licenses/lgpl-license.php. http://www.SOCR.ucla.edu http://wiki.stat.ucla.edu/socr It s Online, Therefore, It Exists! ****************************************************/ public class Main { /****************** CODE EXAMPLE ******************/ // double[] answer = getQuantileArray(testArray); ////////System.out.println("answer = " + answer); //} public static String truncateDigits(double input, int numberDigits) { int indexDot = -1; String integerPart = null, decimalPart = null, dot = "."; String wholePart = null; String result = null; wholePart = input + ""; indexDot = (wholePart).indexOf(dot); if (indexDot >= 0) { integerPart = wholePart.substring(0, indexDot); try { decimalPart = wholePart.substring(indexDot + 1, indexDot + 1 + numberDigits); } catch (Exception e) { decimalPart = wholePart.substring(indexDot + 1, wholePart.length()); //decimalPart = wholePart.substring(indexDot + 1, ); } result = (integerPart + dot + decimalPart).trim(); } else { result = (input + "").trim(); } return result; } public static String[] truncateDigits(double[] input, int numberDigits) { String[] result = new String[input.length]; int indexDot = -1; String integerPart = null, decimalPart = null, dot = "."; String wholePart = null; //int maxNumberDigit = 0; for (int i = 0; i < input.length; i++) { result[i] = truncateDigits(input[i], numberDigits); } return result; } public static String[][] truncateDigits(double[][] input, int numberDigits) { String[][] result = new String[input.length][]; int indexDot = -1; String integerPart = null, decimalPart = null, dot = "."; String wholePart = null; //int maxNumberDigit = 0; for (int i = 0; i < input.length; i++) { result[i] = new String[input[i].length]; for (int j = 0; j < input[i].length; j++) { //////System.out.println("input["+i+"]["+j+"] = " + input[i][j]); wholePart = input[i][j] + ""; indexDot = (wholePart).indexOf(dot); if (indexDot >= 0) { integerPart = wholePart.substring(0, indexDot); try { decimalPart = wholePart.substring(indexDot + 1, indexDot + 1 + numberDigits); } catch (Exception e) { decimalPart = wholePart.substring(indexDot + 1, wholePart.length()); //decimalPart = wholePart.substring(indexDot + 1, ); } result[i][j] = (integerPart + dot + decimalPart).trim(); } else { result[i][j] = (input[i][j] + "").trim(); } } } return result; } }