Example usage for java.lang Math ceil

List of usage examples for java.lang Math ceil

Introduction

In this page you can find the example usage for java.lang Math ceil.

Prototype

public static double ceil(double a) 

Source Link

Document

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

Usage

From source file:Main.java

public static int convertDpToPx(Context context, int dp) {
    final float density = context.getResources().getDisplayMetrics().density;
    return (int) Math.ceil(dp * density);
}

From source file:Main.java

public static int dp2px(int dp, Resources res) {
    final float scale = res.getDisplayMetrics().density;

    return (int) Math.ceil(dp * scale);
}

From source file:Main.java

public static Bitmap createPOT(Bitmap bmp) {
    Bitmap potBmp = null;//from w  ww . ja v  a  2  s.c  o m
    int potWidth = (int) Math.ceil(Math.log(bmp.getWidth()) / Math.log(2));
    int potHeight = (int) Math.ceil(Math.log(bmp.getHeight()) / Math.log(2));
    potHeight = (int) Math.pow(2, potHeight);
    potWidth = (int) Math.pow(2, potWidth);

    if (potWidth == 1) {
        potWidth = 2;
    }
    if (potHeight == 1) {
        potHeight = 2;
    }

    if (potHeight != bmp.getHeight() || potWidth != bmp.getWidth()) {
        int[] colors = new int[potWidth * potHeight];
        int index = 0;

        int offset = potHeight - bmp.getHeight();
        for (int i = 0; i < potHeight; i++) {
            for (int j = 0; j < potWidth; j++) {
                if (i > offset - 1) {
                    if (j < bmp.getWidth() && i < bmp.getHeight()) {
                        colors[index] = bmp.getPixel(j, i);
                    } else {
                        colors[index] = 0;
                    }
                }
                index++;
            }
        }

        potBmp = Bitmap.createBitmap(colors, potWidth, potHeight, bmp.getConfig());
    } else {
        potBmp = bmp;
    }

    System.gc();

    return potBmp;
}

From source file:Main.java

public static int getAlarmVolumeFromPercentage(AudioManager audioManager, int audioStream, float percentage) {
    int volume = (int) Math.ceil((double) audioManager.getStreamMaxVolume(audioStream) * (percentage / 100.0d));
    return volume;
}

From source file:Main.java

/**
 * rounds the given number to the next significant number
 *
 * @param number//from w  ww . jav  a  2 s  .c om
 * @return
 */
public static float roundToNextSignificant(double number) {
    final float d = (float) Math.ceil((float) Math.log10(number < 0 ? -number : number));
    final int pw = 1 - (int) d;
    final float magnitude = (float) Math.pow(10, pw);
    final long shifted = Math.round(number * magnitude);
    return shifted / magnitude;
}

From source file:Main.java

public static int getFontHeight(float fontSize) {
    Paint paint = new Paint();
    paint.setTextSize(fontSize);/*w  ww .  j  av a 2s  . c  o  m*/
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.descent - fm.top) + 2;
}

From source file:Main.java

public static String getMatchingThresholdToString(int value) {
    double p = -value / 12.0;
    NumberFormat nf = NumberFormat.getPercentInstance();
    nf.setMaximumFractionDigits(Math.max(0, (int) Math.ceil(-p) - 2));
    nf.setMinimumIntegerDigits(1);//ww w  .j a v  a 2  s.  c o m
    return nf.format(Math.pow(10, p));
}

From source file:Main.java

/**
 * Computes Mystic Armor given an attribute value
 * @param indice the attribute value//from  w  ww . j a va2s .co  m
 * @return Wound Threshold for the given attribute value
 */
public static int computeMysticArmor(final int indice) {
    return (int) Math.ceil(Math.max(indice - 10, 0) / 3.0);
}

From source file:Main.java

/**
 * Computes Defence value given an attribute value
 * @param indice the attribute value/*from  w  w w  . j a  v  a2 s .  c  om*/
 * @return Defence value for the given attribute value
 */
public static int computeIndiceDefense(final int indice) {
    return (int) Math.ceil(indice / 2.0 + 1.5 - (indice + 1) / 7 / 2.0);
}

From source file:Main.java

public static int measureTextHeight(int textsize) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textsize);/*w  w w .  j  a  v  a  2s .  c om*/
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return (int) Math.ceil(metrics.descent - metrics.ascent);
}