Java Double Number Round round(final double num, final int prec)

Here you can find the source of round(final double num, final int prec)

Description

round

License

Open Source License

Declaration

public static double round(final double num, final int prec) 

Method Source Code

//package com.java2s;
/*/*from   w ww  . j a  v a2 s  .co  m*/
 * Copyright (C) Justo Montiel, David Torres, Sergio Gomez, Alberto Fernandez
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see
 * <http://www.gnu.org/licenses/>
 */

public class Main {
    public static double round(final double num, final int prec) {
        final double epsilon = 1.0e-4;
        double r, rs, x, result;
        double factor;

        factor = Math.pow(10, prec);
        x = num * factor;
        r = Math.round(x);
        if (r < x) {
            rs = Math.round(x + epsilon);
            if (rs - r > 0.5) {
                r = rs;
            }
        }
        return r / factor;
    }
}

Related

  1. round(double zahl, int stellen)
  2. round(double[] arr)
  3. round(double[] array, int decimals)
  4. round(double[][] data)
  5. round(final double num)
  6. round(final double numberToRound, final int decimalPlaces)
  7. round(final double v)
  8. round(final double val, final int decimalPlaces)
  9. round(final double val, final int decimals)