Java Double Number Round round(double value)

Here you can find the source of round(double value)

Description

Rounds the given double value

License

Open Source License

Parameter

Parameter Description
value the value

Return

the rounded value, x.5 and higher is rounded to x + 1.

Declaration

public static long round(double value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot.// w w  w.  ja  v a2 s  . c  o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * Rounds the given double value
     * 
     * @param value
     *            the value
     * @return the rounded value, x.5 and higher is rounded to x + 1.
     * @since CLDC 1.1
     */
    public static long round(double value) {
        if (value < 0) {
            return (long) (value - 0.5);
        } else {
            return (long) (value + 0.5);
        }
    }

    public static double round(double value, int places) {
        if (places < 0) {
            throw new IllegalArgumentException();
        }

        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = Math.round(value);
        return (double) tmp / factor;
    }
}

Related

  1. round(double val, int places)
  2. round(double val, int places)
  3. round(double val, int places)
  4. round(double val, long dec)
  5. round(double valor)
  6. round(double value)
  7. round(double value)
  8. round(double value)
  9. round(double value)