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 getBrightness(int red, int green, int blue) {
    //Color brightness is determined by the following formula:
    //((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
    return Math.round(((red * 299) + (green * 587) + (blue * 114)) / 1000);
}

From source file:Main.java

public static int dpToPx(int dp, Context context) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

From source file:Main.java

public static int pxToDp(int px, Context context) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

From source file:Main.java

public static int ConvertToDp(int px) {

    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return Math.round(dp);
}

From source file:Main.java

public static int dpToPx(Context context, int dp) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

From source file:Main.java

/**
 * @param originalColor color, without alpha
 * @param alpha         from 0.0 to 1.0//from   w ww  .ja  v  a 2  s.  c o  m
 * @return
 */
public static String addAlpha(String originalColor, double alpha) {
    long alphaFixed = Math.round(alpha * 255);
    String alphaHex = Long.toHexString(alphaFixed);
    if (alphaHex.length() == 1) {
        alphaHex = "0" + alphaHex;
    }
    originalColor = originalColor.replace("#", "#" + alphaHex);

    return originalColor;
}

From source file:Main.java

public static int getPixelsInt(Context context, float dp) {
    Resources r = context.getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}

From source file:Main.java

public static float yiweixiaoshu2(String value) {
    if ("0".equals(value) || "0.0".equals(value) || "0.00".equals(value)) {
        return 0f;
    }//from w  w  w.  j  a  v  a 2 s  .c  o  m
    float a = Float.parseFloat(value);
    float b = (float) (Math.round(a * 10)) / 10;
    return b;
}

From source file:Main.java

public static int[] getScreenDP(Context context) {
    int[] screenSize = new int[2];
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    screenSize[0] = Math.round(displayMetrics.widthPixels / displayMetrics.density);
    screenSize[1] = Math.round(displayMetrics.heightPixels / displayMetrics.density);
    return screenSize;
}

From source file:Main.java

public static int getDip(Resources res, int resId) {
    return Math.round(res.getDimension(resId));
}