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:Com.Operaciones.java

public static double Redondear(double NDecimal, int decimales) {
    return Math.round(NDecimal * Math.pow(10, decimales)) / Math.pow(10, decimales);
}

From source file:Timing.java

public static double round1(double value) {
    return Math.round(value * 10.0) / 10.0;
}

From source file:Main.java

public static long calculateWeiboLength(CharSequence c) {

    double len = 0;
    for (int i = 0; i < c.length(); i++) {
        int temp = (int) c.charAt(i);
        if (temp > 0 && temp < 127) {
            len += 0.5;//from   w w  w. j ava 2  s.  com
        } else {
            len++;
        }
    }
    return Math.round(len);
}

From source file:Main.java

/**
 * Method returning color between start and end color proportional to given values.
 *
 * @param colorStart start color/*  w w  w .j  a va2 s .c  o  m*/
 * @param colorEnd   end color
 * @param fullValue  total value
 * @param partValue  part of fullValue. When partValue equals 0, returning color is colorStart,
 *                   when partValue is fullValue returning color is endColor. Otherwise returning
 *                   color is from between those, relative to partValue/fullValue ratio.
 * @return color from between start and end color relative to partValue/fullValue ratio.
 */
public static int getProportionalColor(int colorStart, int colorEnd, float fullValue, float partValue) {
    float progress = Math.min(Math.max(partValue, 0f), fullValue) / fullValue;
    return Color.argb(Math.round(Color.alpha(colorStart) * (1 - progress) + Color.alpha(colorEnd) * progress),
            Math.round(Color.red(colorStart) * (1 - progress) + Color.red(colorEnd) * progress),
            Math.round(Color.green(colorStart) * (1 - progress) + Color.green(colorEnd) * progress),
            Math.round(Color.blue(colorStart) * (1 - progress) + Color.blue(colorEnd) * progress));
}

From source file:Main.java

public static int calculateInSampleSize(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   w  ww . j a  v a  2 s .c  o m
    return inSampleSize;
}

From source file:Main.java

/**
 * Adjust the alpha of a color./*from   w ww .  j a v a2  s .  c o m*/
 * @param color the color [0x00000000, 0xffffffff]
 * @param factor the factor for the alpha [0,1]
 * @return the adjusted color
 */
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 Bitmap bitmapLoad(Resources res, int resId, int width, int height) {

    // calc scale, load appropriately sampled bitmap from given resource

    BitmapFactory.Options resOptions = new BitmapFactory.Options();
    resOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, resOptions);

    int resHeight = resOptions.outHeight;
    int resWidth = resOptions.outWidth;

    float xScale = (float) width / (float) resWidth;
    float yScale = (float) height / (float) resHeight;
    float scale = Math.max(xScale, yScale);
    if (scale > 1)

        if (width == 0)
            width = Math.round(resWidth / scale);
        else if (height == 0)
            height = Math.round(resHeight / scale);

    resOptions.inSampleSize = sampleSize(scale);
    resWidth /= resOptions.inSampleSize;
    resHeight /= resOptions.inSampleSize;
    resOptions.inJustDecodeBounds = false;

    Bitmap rawBitmap = BitmapFactory.decodeResource(res, resId, resOptions);

    // compare aspect ratio and crop

    rawBitmap = bitmapCrop(rawBitmap, width, height, resWidth, resHeight);

    // scale to desired size

    return Bitmap.createScaledBitmap(rawBitmap, width, height, true);
}

From source file:Main.java

/**
 * /* w  w  w  .  j  ava 2 s  .  c  om*/
 * @param file
 */
public static void decodeFile(File file) {
    Bitmap bitmap = null;

    try {
        // Decode image size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        FileInputStream fileInputStream = new FileInputStream(file);
        BitmapFactory.decodeStream(fileInputStream, null, options);
        fileInputStream.close();

        int scale = 1;

        if (options.outHeight > 500 || options.outWidth > 500) {
            scale = (int) Math.pow(2, (int) Math.round(
                    Math.log(500 / (double) Math.max(options.outHeight, options.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options options2 = new BitmapFactory.Options();
        options2.inSampleSize = scale;

        fileInputStream = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fileInputStream, null, options2);
        fileInputStream.close();

        FileOutputStream output = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, output);

        output.flush();
        output.close();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

From source file:Main.java

public static int dpToPixels(final Context context, final int dp) {
    if (mScreenDensityFactorDP == -1f) {
        mScreenDensityFactorDP = context.getResources().getDisplayMetrics().densityDpi;
    }/*  w w  w. ja v a2  s  . co m*/
    return Math.round(dp * mScreenDensityFactorDP / 160f);
}

From source file:Main.java

public static int pixelsToDP(final Context context, final int pixels) {
    if (mScreenDensityFactorDP == -1) {
        mScreenDensityFactorDP = context.getResources().getDisplayMetrics().densityDpi;
    }/*ww  w  . j  av  a  2  s.  c o m*/
    return Math.round(pixels / mScreenDensityFactorDP / 160f);
}