Java Number Round roundToDecimalPlaces(final double val, final int places)

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

Description

Returns the given value rounded to the given precision.

License

Apache License

Parameter

Parameter Description
val the value to be rounded
places the number of places to round to

Return

the rounded value

Declaration

public static double roundToDecimalPlaces(final double val, final int places) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from ww  w  .  j  av  a  2 s .co  m
     * Returns the given value rounded to the given precision.
     *
     * @param val the value to be rounded
     * @param places the number of places to round to
     * @return the rounded value
     */
    public static double roundToDecimalPlaces(final double val, final int places) {
        final long power = (long) Math.pow(10, places);
        return ((long) (val * power + 0.5)) / (double) power;
    }
}

Related

  1. roundToAccuracy(float n, float i)
  2. roundToBase(int a, int base)
  3. roundToBytes(int bitWidth)
  4. roundToDay(long time)
  5. roundToDecentPrecision(double value)
  6. roundToDecimals(double d, int c)
  7. roundToDecimals(double d, int c)
  8. roundToDecimals(double d, int c)
  9. roundToDecimals(double d, int numberOfDecimalPlaces)