Java Double Number Round round(double val, int places)

Here you can find the source of round(double val, int places)

Description

AUTHOR : Ravi Kiran DATE : 18-08-06 NAME : round DESCRIPTION : Round a double value to a specified number of decimal places ARGUMENTS : val - the value to be rounded places - the number of decimal places to round to RETURNS : val - rounded to places decimal places NOTES : None CHANGE HISTROY Author Date Description

License

Open Source License

Declaration

private static double round(double val, int places) 

Method Source Code

//package com.java2s;
/*****************************************************************************************
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:/*from  w  ww.  ja  v  a2s.  c o m*/
 *
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *****************************************************************************************/

public class Main {
    /**********************************************************************************
     * AUTHOR      : Ravi Kiran
     * DATE         : 18-08-06
     * NAME         : round
     * DESCRIPTION   : Round a double value to a specified number of decimal places
     * ARGUMENTS   : val - the value to be rounded
     *               places - the number of decimal places to round to
     * RETURNS      : val - rounded to places decimal places
     * NOTES      : None
     * CHANGE HISTROY
     * Author         Date            Description 
     *************************************************************************************/
    private static double round(double val, int places) {
        long factor = 0;
        long tmp = 0;
        try {
            factor = (long) Math.pow(10, places);

            // Shift the decimal the correct number of places
            // to the right.
            val = val * factor;

            // Round to the nearest integer.
            tmp = Math.round(val);
        } catch (Exception exception) {
            System.err
                    .println("JAVA UI Interface: Exception occured in LipiTKUtil while triming float value :"
                            + exception.toString());
        }

        // Shift the decimal the correct number of places
        // back to the left.
        return (double) tmp / factor;
    }

    /**********************************************************************************
     * AUTHOR      : Ravi Kiran
     * DATE         : 18-08-06
     * NAME         : round
     * DESCRIPTION   : Round a float value to a specified number of decimal places
     * ARGUMENTS   : val - the value to be rounded
     *               places - the number of decimal places to round to
     * RETURNS      : val - rounded to places decimal places
     * NOTES      : None
     * CHANGE HISTROY
     * Author         Date            Description 
     *************************************************************************************/
    public static float round(float val, int places) {
        return (float) round((double) val, places);
    }
}

Related

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