Java Number Round roundToNthDecimal(double number, int decimals)

Here you can find the source of roundToNthDecimal(double number, int decimals)

Description

round To Nth Decimal

License

Open Source License

Declaration

public static double roundToNthDecimal(double number, int decimals) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double roundToNthDecimal(double number, int decimals) {
        return roundToNearest(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
    }/*from   w  ww  . ja v  a 2 s.  c o m*/

    public static double roundToNearest(double number) {
        if ((int) (number + .5) >= (int) (number))
            return (int) number + 1;
        return (int) number;
    }
}

Related

  1. roundToNearestMultipleOf(final long num, final long multipleOf)
  2. roundToNearestN(float value, float n)
  3. roundToNearestPowerOfTen(double value)
  4. roundToNext(int val, int factor)
  5. roundToNextPowerOfTwo(int value)
  6. roundToNumDigits(double d, int n)
  7. roundToPowerOf(int i, int powerOf)
  8. roundToPowerOf10(double value, int powerOf10)
  9. roundToPowerOf2(int value)