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

public static int[] getFeature3(int[] pixels, int width, int height, int bWidth, int bHeight) {

    int[] feature = null;
    if (height % bHeight == 0 && width % bWidth == 0) {
        int numBlockH = height / bHeight;
        int numBlockW = width / bWidth;
        int totalBlocks = numBlockH * numBlockW;
        feature = new int[totalBlocks];
        for (int k = 0; k < numBlockH; k++) {
            for (int r = 0; r < numBlockW; r++) {
                int value = 0;
                for (int i = 0; i < bHeight; i++) {
                    for (int j = 0; j < bWidth; j++) {
                        int index = i * width + j + r * bWidth + k * width * bHeight;
                        value += pixels[index];
                    }/*from   w  w  w  . java2s.  co  m*/
                }
                feature[k * numBlockW + r] = Math.round((float) value / (bWidth * bHeight));
            }
        }
    } else {
        System.out.println("errore");
    }

    return feature;
}

From source file:Main.java

private static int calculateInSampleSize(float outWidth, float outHeight, float reqWidth, float reqHeight) {
    int inSampleSize = 1;
    if (reqHeight == 0 || reqWidth == 0) {
        return inSampleSize;
    }//from   www .  ja v a 2s . c  o m
    if (outHeight > reqHeight || outWidth > reqWidth) {
        final int heightRatio = Math.round(outHeight / reqHeight);
        final int widthRatio = Math.round(outWidth / reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    final float totalPixels = outWidth * outHeight;
    final float totalReqPixelsCap = reqWidth * reqHeight * 2;

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

    return inSampleSize;
}

From source file:Util.java

public static String parseDollars(double amount) {
    if (Double.isNaN(amount) || Double.isInfinite(amount))
        return (new Double(amount)).toString();
    return parseDollars((int) Math.round(amount));
}

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) {

        // Calculate ratios of height and width to requested height and
        // width/*w  w  w  . j a v  a  2  s  .c  o  m*/
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

From source file:Main.java

public static <V> V getRandom(Collection<V> collection) {

    if (collection.isEmpty()) {
        return null;
    }/*from  ww w  . j a v a  2 s . com*/

    int randIdx = (int) Math.round(Math.random() * (collection.size() - 1));
    int count = 0;
    Iterator<V> iterator = collection.iterator();
    while (iterator.hasNext()) {
        V current = iterator.next();
        if (count == randIdx) {
            return current;
        }
        count++;
    }

    throw new RuntimeException("Shouldn't happen");
}

From source file:Main.java

public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
    // read in the dimensions of the image on disk
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w w w. ja  va  2  s  . co m*/
    BitmapFactory.decodeFile(path, options);

    float srcWidth = options.outWidth;
    float srcHeight = options.outHeight;

    int inSampleSize = 1;
    if (srcHeight > destHeight || srcWidth > destWidth) {
        if (srcWidth > srcHeight) {
            inSampleSize = Math.round(srcHeight / destHeight);
        } else {
            inSampleSize = Math.round(srcWidth / destWidth);
        }
    }

    options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    return BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

/**
 * Returns a density independent pixel value rounded to the nearest integer
 * for the desired dimension./*from  w ww. j av  a 2  s. co m*/
 *
 * @param pixels The pixel value to be converted
 * @return A rounded DIP value
 */
public static int convertToDIP(int pixels) {
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pixels,
            Resources.getSystem().getDisplayMetrics()));
}

From source file:Main.java

public static int resizeTextSize(int screenWidth, int screenHeight, int textSize) {
    float ratio = 1;
    try {/*from   ww w  . ja va 2  s .  c  o  m*/
        float ratioWidth = (float) screenWidth / 480;
        float ratioHeight = (float) screenHeight / 800;
        ratio = Math.min(ratioWidth, ratioHeight);
    } catch (Exception e) {
    }
    return Math.round(textSize * ratio);
}

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) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }/*from  ww  w.  j a v 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

public static String toNumberPassword(int length) {
    if (length < 1) {
        return null;
    }//ww  w . ja  v  a  2s. c o m
    String strChars[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    StringBuffer strPassword = new StringBuffer();
    int nRand = (int) Math.round(Math.random() * 100D);
    for (int i = 0; i < length; i++) {
        nRand = (int) Math.round(Math.random() * 100D);
        strPassword.append(strChars[nRand % (strChars.length - 1)]);
    }
    return strPassword.toString();
}