Java Double Number Truncate truncate(final double value, final double precision)

Here you can find the source of truncate(final double value, final double precision)

Description

Truncate a value to the specified precision.

License

LGPL

Parameter

Parameter Description
value the value
precision a precision index which denotes powers of e

Return

the truncated value.

Declaration

public static final double truncate(final double value, final double precision) 

Method Source Code

//package com.java2s;
/*//from w  w  w  .  j  a va  2s  . c  o  m
 * Copyright (c) 2006 Thomas Weise for sigoa
 * Simple Interface for Global Optimization Algorithms
 * http://www.sigoa.org/
 *
 * E-Mail           : info@sigoa.org
 * Creation Date    : 2006-11-27
 * Creator          : Thomas Weise
 * Original Filename: org.sigoa.refimpl.go.objectives.ObjectiveUtils.java
 * Last modification: 2006-11-27
 *                by: Thomas Weise
 *
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to theFree Software
 *                    Foundation, Inc. 51 Franklin Street, Fifth Floor,
 *                    Boston, MA 02110-1301, USA or download the license
 *                    under http://www.gnu.org/licenses/lgpl.html or
 *                    http://www.gnu.org/copyleft/lesser.html.
 *
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

public class Main {
    /**
     * Truncate a value to the specified precision. The return value will be
     * the input value but truncated to precision powers of e.
     * 
     * @param value
     *          the value
     * @param precision
     *          a precision index which denotes powers of e
     * @return the truncated value.
     */
    public static final double truncate(final double value, final double precision) {
        double f, v;
        boolean b;

        if (precision <= 0.0d)
            return value;

        if (value == 0.0d)
            return 0.0d;

        if (value < 0.0d) {
            v = -value;
            b = true;
        } else {
            v = value;
            b = false;
        }

        f = Math.exp(precision - Math.rint(Math.log(v)));
        v = ((Math.rint(v * f)) / f);
        return (b ? (-v) : v);
    }
}

Related

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