Example usage for java.lang Math round

List of usage examples for java.lang Math round

Introduction

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

Prototype

public static long round(double a) 

Source Link

Document

Returns the closest long to the argument, with ties rounding to positive infinity.

Usage

From source file:Main.java

private static int calculateInSampleSizeForCompression(BitmapFactory.Options options, int reqWidth,
        int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }//  ww w .  jav  a 2 s  .  c o m
    final float totalPixels = width * height;
    final float totalReqPixelsCap = reqWidth * reqHeight * 2;
    while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
        inSampleSize++;
    }

    return inSampleSize;
}

From source file:Main.java

/**
 * Returns a number rounded off to the given number of decimals.
 *
 * @param value    the value to round off.
 * @param decimals the number of decimals.
 * @return a number rounded off to the given number of decimals.
 *///from www  . j  a  v  a2 s .c  om
public static double getRounded(double value, int decimals) {
    final double factor = Math.pow(10, decimals);

    return Math.round(value * factor) / factor;
}

From source file:Main.java

public static int getScreenWidth(Activity ctx) {
    Display display = ctx.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);/*from ww w .  j a v  a2  s  .  c om*/

    float density = ctx.getResources().getDisplayMetrics().density;
    return Math.round(outMetrics.widthPixels);

}

From source file:Main.java

public static int getGeoHash(double lat, double lng) {
    if (lat == 0.0 && lng == 0.0)
        return 0;

    double latHash = (lat + 180) / LATLON_SECTION;
    double lngHash = (lng + 180) / LATLON_SECTION;
    int latHashRounded = (int) Math.round(latHash);
    int lngHashRounded = (int) Math.round(lngHash);

    int hash = latHashRounded * 10000 + lngHashRounded;

    Log.d(TAG, "HotspotDbHelper getGeoHash for lat: " + lat + " lon: " + lng + " is " + hash);
    return hash;/*w w w  .  j  a  v a  2  s  .c  o  m*/
}

From source file:Main.java

/**
 * Alters the alpha of a given color and spits out the new color.
 * @param color the color to change the alpha of.
 * @param value the new value of the alpha field.
 * @return the new color with the new alpha level.
 *///from   ww  w.java2s.co  m
public static int getNewColorAlpha(int color, float value) {
    int alpha = Math.round(Color.alpha(color) * value);
    int r = Color.red(color);
    int g = Color.green(color);
    int b = Color.blue(color);
    return Color.argb(alpha, r, g, b);
}

From source file:Main.java

public static int getUPSRuntime(float totalWatts, int vah, int ptare, float bCurve_scale, float bCurve_expo,
        float bCurve_rSlope, float bCurve_comp, int max_runtime) {
    if ((vah <= 0) || (totalWatts < 0) || (ptare < 0) || (totalWatts + ptare == 0) || (bCurve_scale <= 0)
            || (bCurve_expo <= 0) || (bCurve_rSlope < 0)) {
        return 0;
    } else {/*from  w ww.  j ava2s. co  m*/
        return (int) Math.round(60 * bCurve_scale * (Math.pow(vah / (totalWatts + ptare), bCurve_expo)
                - bCurve_rSlope + bCurve_comp * (totalWatts + ptare) / vah));
    }
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    int inSampleSize = 1;
    double ratioH = (double) options.outHeight / reqHeight;
    double ratioW = (double) options.outWidth / reqWidth;

    int h = (int) Math.round(ratioH);
    int w = (int) Math.round(ratioW);

    if (h > 1 || w > 1) {
        if (h > w) {
            inSampleSize = h >= 2 ? h : 2;

        } else {/*from   www.  j a  va  2  s. c o  m*/
            inSampleSize = w >= 2 ? w : 2;
        }
    }
    return inSampleSize;
}

From source file:Main.java

public static String getRandomString(int length) {
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(3);
        long result = 0;
        switch (number) {
        case 0://from  w  w  w.j  a v a  2 s.c om
            result = Math.round(Math.random() * 25 + 65);
            sb.append(String.valueOf((char) result));
            break;
        case 1:
            result = Math.round(Math.random() * 25 + 97);
            sb.append(String.valueOf((char) result));
            break;
        case 2:
            sb.append(String.valueOf(new Random().nextInt(10)));
            break;
        }

    }
    return sb.toString();
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {/*w  w w.  j  a  va2 s  . c  om*/
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

From source file:Main.java

public static String truncateChange(String change, boolean isPercentChange) {
    String weight = change.substring(0, 1);
    String ampersand = "";
    if (isPercentChange) {
        ampersand = change.substring(change.length() - 1, change.length());
        change = change.substring(0, change.length() - 1);
    }// w  ww .  j a va 2s  .  c  o m
    change = change.substring(1, change.length());
    double round = (double) Math.round(Double.parseDouble(change) * 100) / 100;
    change = String.format("%.2f", round);
    StringBuffer changeBuffer = new StringBuffer(change);
    changeBuffer.insert(0, weight);
    changeBuffer.append(ampersand);
    change = changeBuffer.toString();
    return change;
}