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 pxFromDp(final Context context, final float dp) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return px;//w  ww.j  a  v a2 s.  c o m
}

From source file:Main.java

/**
 * Convert DP to pixels using the device screen density
 *//*from  w  w  w. j  ava 2 s.  co  m*/
public static int dpToPx(int dp) {
    float density = Resources.getSystem().getDisplayMetrics().density;
    return Math.round(dp * density);
}

From source file:Main.java

public static int latitudeToPixelY(double latitude, double zoomLevel, int tileSize) {
    double sinLatitude = Math.sin(latitude * (Math.PI / 180));
    int mapSize = getMapSize(zoomLevel, tileSize);
    return (int) Math
            .round((0.5d - Math.log((1d + sinLatitude) / (1d - sinLatitude)) / (4d * Math.PI)) * mapSize);
}

From source file:Main.java

public static Bitmap scaleDown(Bitmap bmp, float maxImgSize, boolean filter) {
    float ratio = Math.min((float) maxImgSize / bmp.getWidth(), (float) maxImgSize / bmp.getHeight());
    int width = Math.round((float) ratio * bmp.getWidth());
    int height = Math.round((float) ratio * bmp.getHeight());

    return Bitmap.createScaledBitmap(bmp, width, height, filter);
}

From source file:Main.java

public static String getSizeStr(long fileLength) {
    String strSize = "";
    try {/*from w  w  w  .  ja  v a2  s . com*/
        if (fileLength >= 1024 * 1024 * 1024) {
            strSize = (float) Math.round(10 * fileLength / (1024 * 1024 * 1024)) / 10 + " GB";
        } else if (fileLength >= 1024 * 1024) {
            strSize = (float) Math.round(10 * fileLength / (1024 * 1024 * 1.0)) / 10 + " MB";
        } else if (fileLength >= 1024) {
            strSize = (float) Math.round(10 * fileLength / (1024)) / 10 + " KB";
        } else if (fileLength >= 0) {
            strSize = fileLength + " B";
        } else {
            strSize = "0 B";
        }
    } catch (Exception e) {
        e.printStackTrace();
        strSize = "0 B";
    }
    return strSize;
}

From source file:Main.java

public static int dp2px(int dp) {
    return Math.round(dp * DENSITY);
}

From source file:Main.java

public static int adjustAlpha(int color, float factor) {
    int alpha = Math.round(Color.alpha(color) * factor);
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);

    return Color.argb(alpha, red, green, blue);
}

From source file:Main.java

public static int dpToPixels(int dp) {
    DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics));
    //return Math.round(dp * Resources.getSystem().getDisplayMetrics().density);
}

From source file:Main.java

/**
 * Prepare the weather high/lows for presentation.
 *//*from   w w  w  .  j a  va2 s .c o  m*/
public static String formatHighLows(double high, double low) {
    // For presentation, assume the user doesn't care about tenths of a degree.
    long roundedHigh = Math.round(high);
    long roundedLow = Math.round(low);

    String highLowStr = roundedHigh + "/" + roundedLow;
    return highLowStr;
}

From source file:Main.java

public static String addUnit(float distance) {
    if (distance == 0) {
        return "0M";
    } else {//  w ww.j  a v  a2 s . com
        if (distance > 1000) {
            distance = distance / 1000;
            distance = (float) ((double) Math.round(distance * 100) / 100);
            return distance + "KM";
        } else {
            distance = (float) ((double) Math.round(distance * 100) / 100);
            return distance + "M";
        }
    }
}