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 Date toModifiedTimeDate(String modified) {
    long now = System.currentTimeMillis();
    try {//  ww w.  ja  va  2  s .c om
        double modDouble = Double.parseDouble(modified) * 1000;
        long mod = Math.round(modDouble);
        //      Dbg.printf("mod: %d ; cur : %d ; delta : %d\n", mod, now, now - mod);
        return new Date(mod);
    } catch (Exception e) {
        return new Date(); // todo buggy ?
    }
}

From source file:Main.java

public static double roundSignificant(double value) {
    if (value < 1.0) {
        return Math.round(value * 100) / 100.0;
    } else {//from  w  ww .j a  v a2 s.  co m
        return Math.round(value * 10) / 10.0;
    }
}

From source file:Main.java

private static int getTimeDistanceInMinutes(long time) {
    long timeDistance = currentDate().getTime() - time;
    return Math.round((Math.abs(timeDistance) / 1000) / 60);
}

From source file:Main.java

static double roundOff(double x, int position) {
    double a = x;
    double temp = Math.pow(10.0, position);
    a *= temp;/*  w  w w . j av  a 2s  .c o  m*/
    a = Math.round(a);
    return (a / (double) temp);
}

From source file:Main.java

public static float roundHour(float time) {
    time *= 10;
    time = Math.round(time);
    time /= 10;
    return time;
}

From source file:Main.java

/** Converts dips into pixels using the {@link Context}'s density. */
public static int convertDipToPixels(Context context, int dip) {
    float density = context.getResources().getDisplayMetrics().density;
    return Math.round(density * dip);
}

From source file:Main.java

private static Bitmap resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) {
    final int width = Math.round(bitmap.getWidth() * scale);
    final int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    final Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);/*from  www  .  j a v a  2 s .  co  m*/
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle) {
        bitmap.recycle();
    }
    return target;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { // BEST QUALITY MATCH
    //First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*  www .  j  a  v a2  s.c  o  m*/
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Config.RGB_565;
    int inSampleSize = 1;
    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }
    int expectedWidth = width / inSampleSize;
    if (expectedWidth > reqWidth) {
        //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }
    options.inSampleSize = inSampleSize;
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return bitmapResult = BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

/**
 * Calculate an acceptable size based on a requested size.
 *
 * @param options Bitmap options./*from w w  w.  j a va2 s .co  m*/
 * @param reqWidth Requested width.
 * @param reqHeight Requested height.
 * @return Sample size based on requested dimensions.
 */
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;

    // Initialize sample size.
    int inSampleSize = 1;

    // If image is larger than requested in at least one dimension.
    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width.
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as sample size 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 sample size.
    return inSampleSize;
}

From source file:Main.java

public static Date toModifiedTimeDate(String modified) {
    @SuppressWarnings("unused")
    long now = System.currentTimeMillis();
    try {//from   ww w  .  j a  v  a 2  s  .  c  o m
        double modDouble = Double.parseDouble(modified) * 1000;
        long mod = Math.round(modDouble);
        //      Dbg.printf("mod: %d ; cur : %d ; delta : %d\n", mod, now, now - mod);
        return new Date(mod);
    } catch (Exception e) {
        return new Date(); // todo buggy ?
    }
}