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 convertSpToPixel(int sp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return Math.round(sp * metrics.scaledDensity);
}

From source file:Main.java

public static String approximate(int number) {
    if (number == 0) {
        return null;
    }/*from   w  w  w .j a v a 2 s .  co  m*/
    if (number < 1000) {
        return String.valueOf(number);
    } else if (number < 10000) {
        float f = number * 1f / 1000;
        return String.valueOf(Math.round(f * 10) / 10.0) + "k";
    } else {
        float f = number * 1f / 10000;
        return String.valueOf(Math.round(f * 10) / 10.0) + "w";
    }
}

From source file:Main.java

public static int convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int px = Math.round(dp * (metrics.densityDpi / 160f)); // bit fuzzy, but probably won't be big issues.
    return px;/*from   ww w.  j a v a 2  s .c om*/
}

From source file:Main.java

public static float per(String string, String string2) {
    try {/* w  w  w.jav  a2  s.  co  m*/
        float retail = Float.valueOf(string);
        float tariff = Float.valueOf(string2);

        float result = (((tariff - retail) / tariff) * 100);
        //         Margin = ((T-R)/ T)*100%
        return Math.round(result * 100) / 100f;
    } catch (Exception e) {
    }
    return 0;
}

From source file:Main.java

/**
 * Function that rounds a number to N decimal places
 * /*  w w w  . jav  a  2 s .  c  o m*/
 * @param d
 *            Double to round
 * @param n
 *            Number of decimal places to round to
 * @return
 */
public static double roundn(double d, int n) {
    d = d * Math.pow(10, n);
    d = Math.round(d);
    d = d / Math.pow(10, n);
    return d;
}

From source file:Main.java

/**
 * dp to px//from w  w  w  .j a va  2  s.  c  o  m
 * @param dp
 * @param mContext
 * @return
 */
public static int dp2px(int dp, Context mContext) {
    DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return px;
}

From source file:Main.java

public static Bitmap CompressBitmap(Bitmap image) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scale = 500.0f / height;
    if (width > 500 || height > 500) {
        width = Math.round(width * scale);
        height = Math.round(height * scale);
    }/*from   w  w w  . ja  v  a2 s . c  o m*/
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    return scaledBitmap;
}

From source file:Main.java

public static int getFitInSampleSize(int reqWidth, int reqHeight, BitmapFactory.Options options) {
    int inSampleSize = 1;
    if (options.outWidth > reqWidth || options.outHeight > reqHeight) {
        int widthRatio = Math.round((float) options.outWidth / (float) reqWidth);
        int heightRatio = Math.round((float) options.outHeight / (float) reqHeight);
        inSampleSize = Math.min(widthRatio, heightRatio);
    }//from w  w  w  . j a va 2  s . c  o  m
    return inSampleSize;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Format distance to be displayed to the user
 *
 * @param d , distance in metres/*w w  w.j a va2s  . co  m*/
 *
 * @return formatted {@link String}
 */
public static String formatDistance(double d) {
    DecimalFormat formatter = new DecimalFormat("#,### 'm'");
    d = Math.round(d);
    if (d > 1000) {
        formatter = new DecimalFormat("#,###.# 'km'");
        d = d / 1000;
    }

    return formatter.format(d);
}