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 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  ww.j  a v  a  2s. c  o m*/
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

From source file:Main.java

public static int getMetricsDensity(Context context, float height) {
    DisplayMetrics localDisplayMetrics = new DisplayMetrics();
    ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
            .getMetrics(localDisplayMetrics);
    return Math.round(height * localDisplayMetrics.densityDpi / 160.0F);
}

From source file:Main.java

public static int calculateInSampleSize(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) {

        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }// w  w w.j av  a 2  s. co m

    return inSampleSize;
}

From source file:Main.java

public static double round(double amount, int i) {
    double d = Math.pow(10.0D, i);
    return (Math.round(amount * d) / d);
}

From source file:Main.java

public static String generateHashWhereClause(double lat, double lng) {
    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 iHash = getHash(latHashRounded, lngHashRounded);
    String whereClause = "hash = " + iHash;

    int nearbyLat = 0;
    int nearbyLng = 0;
    double overlap = LATLON_SECTION_EDGE;
    if (Math.abs(latHash - latHashRounded) > (0.5 - overlap))
        nearbyLat = (int) Math.signum(latHash - latHashRounded);

    if (Math.abs(lngHash - lngHashRounded) > (0.5 - overlap))
        nearbyLng = (int) Math.signum(lngHash - lngHashRounded);

    if (nearbyLat != 0)
        whereClause += " OR hash = " + getHash(latHashRounded + nearbyLat, lngHashRounded);
    if (nearbyLng != 0)
        whereClause += " OR hash = " + getHash(latHashRounded, lngHashRounded + nearbyLng);
    if ((nearbyLat != 0) && (nearbyLng != 0))
        whereClause += " OR hash = " + getHash(latHashRounded + nearbyLat, lngHashRounded + nearbyLng);

    Log.d(TAG,/* w  w  w . j  av  a2s.com*/
            "HotspotDbHelper generateHashWhereClause lat: " + lat + " lon: " + lng + " Where: " + whereClause);
    return whereClause;
}

From source file:Main.java

public static int calculateInSampleSize(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) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {//w w w .j a  va 2 s .  com
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }

        final float totalPixels = width * height;
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;

        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
            inSampleSize++;
        }
    }
    return inSampleSize;
}

From source file:Main.java

public static Bitmap blurLight(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE_LIGHT);
    int height = Math.round(image.getHeight() * BITMAP_SCALE_LIGHT);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS_LIGHT);
    theIntrinsic.setInput(tmpIn);/*from   w w  w.  j a  v a  2s.c  o m*/
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

From source file:Main.java

/**
 * Rounds the given number to the given number of significant digits. Based on an answer on <a
 * href="http://stackoverflow.com/questions/202302">Stack Overflow</a>.
 *//*from  www.  j a va 2s. c  o m*/
public static float roundToOneSignificantFigure(double num) {
    final float d = (float) Math.ceil((float) Math.log10(num < 0 ? -num : num));
    final int power = 1 - (int) d;
    final float magnitude = (float) Math.pow(10, power);
    final long shifted = Math.round(num * magnitude);
    return shifted / magnitude;
}

From source file:Main.java

/**
 * Calculate the optimal number of hash functions for the bloom filter using
 * bloom filter size and no of elements inserted
 * //from  www .j  a  v a  2s.  c om
 * @param bloomFilterSize
 *            Bloom Filter vector size
 * @param noOfElements
 *            Expected number of elements inserted to the filter
 * @return Optimal number of hash functions
 */

public static int optimalNoOfHash(int bloomFilterSize, long noOfElements) {
    return (int) Math.round(bloomFilterSize / noOfElements * Math.log(2));
}

From source file:Main.java

/**
 * Convert a dp float value to pixels/*  w  ww.  ja va 2  s .  co m*/
 *
 * @param dp      float value in dps to convert
 * @return DP value converted to pixels
 */
static int dp2px(Context context, float dp) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            context.getResources().getDisplayMetrics());
    return Math.round(px);
}