Java Number Round roundToNDigits(double d, int n)

Here you can find the source of roundToNDigits(double d, int n)

Description

round To N Digits

License

Apache License

Declaration

public static double roundToNDigits(double d, int n) 

Method Source Code

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

public class Main {
    public static double roundToNDigits(double d, int n) {
        if (d == 0)
            return d;
        int log = (int) Math.log10(d);
        int exp = n;
        exp -= log;//from w  w w  .  j  a  v  a 2 s  . co  m
        int ival = (int) (Math.round(d * Math.pow(10, exp)));
        return ival / Math.pow(10, exp);
    }
}

Related

  1. roundToMultiple(int value, int divisor)
  2. roundToMultiple(int value, int multipleOf)
  3. roundToMultipleXLength(int inLength, int multipler)
  4. roundToN(double d, int n)
  5. roundToNDecimalPlaces(final double in, final int n)
  6. roundToNearest(double d)
  7. roundToNearest(double number)
  8. roundToNearest(int num, int nearest)
  9. roundToNearestEven(int mantissa, int numOfBitsToRoundOff)