Java Number Round roundToPowerOf(int i, int powerOf)

Here you can find the source of roundToPowerOf(int i, int powerOf)

Description

round To Power Of

License

Open Source License

Declaration

public static final int roundToPowerOf(int i, int powerOf) 

Method Source Code

//package com.java2s;
// {LICENSE}/*from  w  ww  .  j  a va 2 s.  c o  m*/

public class Main {
    public static final int roundToPowerOf(int i, int powerOf) {
        int j = 0;
        while (true) {
            j++;
            int k = pow(powerOf, j);
            if (k >= i)
                return k;
        }
    }

    public static final int pow(int i, int pow) {
        if (pow == 0 || i == 0)
            return 1;
        int r = i;
        if (pow > 0) {
            for (int j = 0; j < pow; j++) {
                r *= i;
            }
        } else {
            for (int j = 0; j < -pow; j++) {
                r /= i;
            }
        }
        return r;
    }
}

Related

  1. roundToNearestPowerOfTen(double value)
  2. roundToNext(int val, int factor)
  3. roundToNextPowerOfTwo(int value)
  4. roundToNthDecimal(double number, int decimals)
  5. roundToNumDigits(double d, int n)
  6. roundToPowerOf10(double value, int powerOf10)
  7. roundToPowerOf2(int value)
  8. roundToPrecision(double number, int precision)
  9. roundToPrecision(float value, int numDecimalPlaces)