Java Double Number Truncate truncDouble(Double value, int decimals)

Here you can find the source of truncDouble(Double value, int decimals)

Description

Receive a double value and trunc decimals places

License

Open Source License

Parameter

Parameter Description
value Number to truncate.
decimals Numbers of decimal places to keep.

Return

A truncated double value

Declaration

public static double truncDouble(Double value, int decimals) 

Method Source Code

//package com.java2s;
/* This file is part of TraceMetrics
 *
 * TraceMetrics is a trace file analyzer for Network Simulator 3 (www.nsnam.org).
 * The goal is to calculate useful metrics for research and performance measurement.
 * URL: www.tracemetrics.net//  w  w  w . j  a va2s  . co  m
 *
 * Copyright (C) 2012  Luiz Felipe Zafra Saggioro
 * Copyright (C) 2012  Flavio Barbieri Gonzaga
 * Copyright (C) 2012  Reuel Ramos Ribeiro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Receive a double value and trunc decimals places
     *
     * @param value Number to truncate.
     * @param decimals Numbers of decimal places to keep. If a negative number
     * or number zero is request, nothing will happen with number. * *      * If <code>decimals</code> are bigger that the decimals places of
     * number, then, the original number will be return.
     * @return A truncated double value
     * @since 1.2.0
     */
    public static double truncDouble(Double value, int decimals) {

        char[] charValue = value.toString().toCharArray();

        if (value.toString().contains("E")) {
            return value;
        }

        StringBuilder buffer = new StringBuilder(charValue.length);

        int position = 0;
        while (charValue[position] != '.') {
            buffer.append(charValue[position]);
            position++;
        }
        if (position == 0) {
            buffer.append("0");
        }
        position++;

        if ((charValue.length - position) < decimals) {
            //throw new Exception("Insulficient decimals points.");
            buffer.append(".");

            while (decimals > 0 && position < charValue.length) {
                buffer.append(charValue[position]);
                position++;
                decimals--;
            }
            //If a future implementation request one String as return
            while (decimals > 0) {
                buffer.append('0');
                decimals--;
            }

        } else {

            buffer.append(".");

            while (decimals > 0) {
                buffer.append(charValue[position]);
                position++;
                decimals--;
            }
        }
        return Double.parseDouble(buffer.toString());
    }
}

Related

  1. truncateNaN(double[] values)
  2. truncateNoSciNotation(double d, int decimals)
  3. truncateRows(double[][] matrix, int nCols)
  4. truncateToTwoDecimals(double value)
  5. truncDouble(double val)