Java Double Number Truncate truncateDoubleDecimals(double x1, int num)

Here you can find the source of truncateDoubleDecimals(double x1, int num)

Description

truncate Double Decimals

License

Open Source License

Declaration

public static double truncateDoubleDecimals(double x1, int num) 

Method Source Code

//package com.java2s;
/*// www  .  j av a  2  s. co m
 * Copyright (C) 2007 Alberto Duina, SPEDALI CIVILI DI BRESCIA, Brescia ITALY
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

public class Main {
    public static double truncateDoubleDecimals(double x1, int num) {
        double mult1 = Math.pow(10, num);
        int mult = (int) mult1;
        double y1;
        double y2;
        if (x1 > 0) {
            y1 = Math.floor(x1 * mult);
            y2 = y1 / (mult);
        } else {
            y1 = Math.ceil(x1 * mult);
            y2 = y1 / (mult);
        }
        return y2;
    }
}

Related

  1. truncate(final double value, final double precision)
  2. truncate2decimals(double x)
  3. truncateDigits(double input, int numberDigits)
  4. truncateDigits(double[][] input, int numberDigits)
  5. truncateDouble(final Double value)
  6. truncateDoubleToInt(double p_76140_0_)
  7. truncateDoubleToInt(double x)
  8. truncateNaN(double[] values)
  9. truncateNoSciNotation(double d, int decimals)