Java Double Number Truncate truncateDigits(double input, int numberDigits)

Here you can find the source of truncateDigits(double input, int numberDigits)

Description

CODE EXAMPLE

License

Open Source License

Declaration





public static String truncateDigits(double input, int numberDigits) 

Method Source Code

//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;
    }
}

Related

  1. truncate(double x, double gran)
  2. truncate(double[] arr, int m)
  3. truncate(final double value, final double min, final double max)
  4. truncate(final double value, final double precision)
  5. truncate2decimals(double x)
  6. truncateDigits(double[][] input, int numberDigits)
  7. truncateDouble(final Double value)
  8. truncateDoubleDecimals(double x1, int num)
  9. truncateDoubleToInt(double p_76140_0_)