Java Double Number Truncate truncateDouble(final Double value)

Here you can find the source of truncateDouble(final Double value)

Description

Truncate the decimal part of a double to keep only 2 decimals.

License

Open Source License

Parameter

Parameter Description
value Value to truncate (can be <code>null</code>).

Return

The truncated number or null if the given value was null.

Declaration

public static Double truncateDouble(final Double value) 

Method Source Code

//package com.java2s;
/*// w w w.j a  v  a2 s  .co  m
 * #%L
 * Sigmah
 * %%
 * Copyright (C) 2010 - 2016 URD
 * %%
 * 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/gpl-3.0.html>.
 * #L%
 */

public class Main {
    /**
     * Truncate the decimal part of a double to keep only 2 decimals.
     * 
     * @param value
     *         Value to truncate (can be <code>null</code>).
     * @return The truncated number or <code>null</code> if the given value was <code>null</code>.
     */
    public static Double truncateDouble(final Double value) {

        if (value == null) {
            return null;
        }

        return Double.valueOf(truncate(value));
    }

    /**
     * Truncate the decimal part of a number to keep only 2 decimals.
     * 
     * @param n
     *          The number, must not be <code>null</code>.
     * @return The truncated number.
     */
    public static String truncate(Number n) {
        return truncate(n, 2);
    }

    /**
     * Truncate the decimal part of a number.
     * 
     * @param n
     *          The number, must not be <code>null</code>.
     * @param decimals
     *          The number of decimals. <code>0</code> will truncate all the decimal part.
     * @return The truncated number.
     */
    public static String truncate(Number n, int decimals) {

        if (n == null) {
            throw new IllegalArgumentException("n must not be null.");
        }

        if (decimals < 0) {
            throw new IllegalArgumentException("decimals must not be lower than 0.");
        }

        // Retrieves the number as double.
        final Double d = n.doubleValue();
        final String asString = d.toString();

        // Searches the decimal separator.
        final int index = asString.indexOf('.');

        final String truncatedDoubleAsString;

        // If the number as no decimal part, nothing to do.
        if (index == -1) {
            truncatedDoubleAsString = asString;
        }
        // Truncates the decimal part.
        else {

            // Truncates all the decimal part.
            if (decimals == 0) {
                truncatedDoubleAsString = asString.substring(0, index);
            } else {

                final int last = index + 1 + decimals;

                if (last > asString.length()) {
                    truncatedDoubleAsString = asString;
                } else {
                    truncatedDoubleAsString = asString.substring(0, last);
                }
            }
        }

        return truncatedDoubleAsString;
    }
}

Related

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