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

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*from  w  w  w  .  j av a  2 s .c  om*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}

From source file:Main.java

/**
 * For a string containing a number delta as judged by
 * <code>isNumberDelta()</code>, return the numerical value, rounded to
 * an integer./*  ww w  .j  a  va2  s.c  o m*/
 *
 * @return the numeric value, rounded to an integer, or 0
 * if the string is not a valid number delta.
 */
public static int getNumberDelta(String string) {
    String s = string.trim();
    if (!isNumberDelta(s))
        return 0;
    double value = 0;
    try {
        value = Double.parseDouble(s);
    } catch (NumberFormatException e) {
        //logger.warn("Unexpected number value `" + s + "'");
    }
    return (int) Math.round(value);

}

From source file:Main.java

/**
 * For a string containing an unsigned number as judged by
 * <code>isUnsignedNumber()</code>, return the numerical value, rounded to
 * an integer.//from  w  ww  . j a  v  a2s .  c  om
 *
 * @return the numeric value, rounded to an integer, or 0
 * if the string is not a valid unsigned number.
 */
public static int getUnsignedNumber(String string) {
    String s = string.trim();
    if (!isUnsignedNumber(s))
        return 0;
    double value = 0;
    try {
        value = Double.parseDouble(s);
    } catch (NumberFormatException e) {
        //logger.warn("Unexpected number value `" + s + "'");
    }
    return (int) Math.round(value);
}

From source file:Main.java

/**
 * Convert XYZ to RGB.//ww  w  .ja v a 2  s.co  m
 *
 * @param X
 * @param Y
 * @param Z
 * @return RGB in int array.
 */
public static int[] XYZtoRGB(double X, double Y, double Z) {
    int[] result = new int[3];

    double x = X / 100.0;
    double y = Y / 100.0;
    double z = Z / 100.0;

    // [r g b] = [X Y Z][Mi]
    double r = (x * Mi[0][0]) + (y * Mi[0][1]) + (z * Mi[0][2]);
    double g = (x * Mi[1][0]) + (y * Mi[1][1]) + (z * Mi[1][2]);
    double b = (x * Mi[2][0]) + (y * Mi[2][1]) + (z * Mi[2][2]);

    // assume sRGB
    if (r > 0.0031308) {
        r = ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055);
    } else {
        r = (r * 12.92);
    }
    if (g > 0.0031308) {
        g = ((1.055 * Math.pow(g, 1.0 / 2.4)) - 0.055);
    } else {
        g = (g * 12.92);
    }
    if (b > 0.0031308) {
        b = ((1.055 * Math.pow(b, 1.0 / 2.4)) - 0.055);
    } else {
        b = (b * 12.92);
    }

    r = (r < 0) ? 0 : r;
    g = (g < 0) ? 0 : g;
    b = (b < 0) ? 0 : b;

    // convert 0..1 into 0..255
    result[0] = (int) Math.round(r * 255);
    result[1] = (int) Math.round(g * 255);
    result[2] = (int) Math.round(b * 255);

    return result;
}

From source file:Main.java

/**
 * See <a href=/*from  w w w.j  a  va  2s.c  om*/
 * "http://apache-poi.1045710.n5.nabble.com/Excel-Column-Width-Unit-Converter-pixels-excel-column-width-units-td2301481.html"
 * >here</a> for Xio explanation and details
 */
public static int getColumnWidthInPx(int widthUnits) {
    int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR) * UNIT_OFFSET_LENGTH;

    int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
    pixels += Math.round(offsetWidthUnits / ((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH));

    return pixels;
}

From source file:Main.java

/**
 * Calculate an inSampleSize for use in a
 * {@link BitmapFactory.Options} object when decoding
 * bitmaps using the decode* methods from
 * {@link BitmapFactory}. This implementation calculates
 * the closest inSampleSize that will result in the final decoded bitmap
 * having a width and height equal to or larger than the requested width and
 * height. This implementation does not ensure a power of 2 is returned for
 * inSampleSize which can be faster when decoding but results in a larger
 * bitmap which isn't as useful for caching purposes.
 *
 * @param options   An options object with out* params already populated (run
 *                  through a decode* method with inJustDecodeBounds==true
 * @param reqWidth  The requested width of the resulting bitmap
 * @param reqHeight The requested height of the resulting bitmap
 * @return The value to be used for inSampleSize
 *///from w  w w  .  j a v  a  2  s.c o m
public 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;

    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        int widthSampleSize = 0;
        int heightSampleSize = 0;
        if (reqWidth < width) {
            widthSampleSize = Math.round((float) width / (float) reqWidth);
        }
        if (reqHeight < height) {
            heightSampleSize = Math.round((float) height / (float) reqHeight);
        }
        inSampleSize = Math.max(widthSampleSize, heightSampleSize);
    }
    return inSampleSize;
}

From source file:org.zilverline.util.Utils.java

/**
 * Picks a random number between 0 and (below) the givenNumber.
 * /*from w w  w  .j a va2s  .c om*/
 * @param number the givenNumber
 * 
 * @return a number n: 0 &lt;= n &lt; givenNumber
 */
public static int pickOne(final int number) {
    return (int) Math.round(Math.random() * (double) (number - 1));
}

From source file:Main.java

public static int calculateInSampleSize(Activity context, int outWidth, int outHeight) {
    int screenWidth = context.getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = context.getWindowManager().getDefaultDisplay().getHeight();
    int be;/* w  w w. jav  a  2  s .c o  m*/
    if (outWidth > screenWidth || outHeight > 1.5 * screenHeight) {
        int heightRatio = Math.round(((float) outHeight) / ((float) 1.5 * screenHeight));
        int widthRatio = Math.round(((float) outWidth) / ((float) screenWidth));
        int sample = heightRatio > widthRatio ? heightRatio : widthRatio;
        if (sample < 3)
            be = sample;
        else if (sample < 6.5)
            be = 4;
        else if (sample < 8)
            be = 8;
        else
            be = sample;
    } else {
        be = 1;
    }
    if (be <= 0) {
        be = 1;
    }
    return be;
}

From source file:Util.java

/**
 * Retrive the quartile value from an array
 * ./*from   ww w  .  j  a  v  a2  s .  co m*/
 * @param values THe array of data
 * @param lowerPercent The percent cut off. For the lower quartile use 25,
 *      for the upper-quartile use 75
 * @return
 */
public static double quartile(double[] values, double lowerPercent) {

    if (values == null || values.length == 0) {
        throw new IllegalArgumentException("The data array either is null or does not contain any data.");
    }

    // Rank order the values
    double[] v = new double[values.length];
    System.arraycopy(values, 0, v, 0, values.length);
    Arrays.sort(v);

    int n = (int) Math.round(v.length * lowerPercent / 100);

    return v[n];

}

From source file:visual.FinalToolTipChartState.java

@Override
public String generateToolTip(XYDataset xyd, int i, int i1) {
    String s = Double.toString((double) Math.round(xyd.getYValue(i, i1) * 100000) / 100000);
    String actName = svc.getActionNameFor(i1);
    return actName + " (" + String.valueOf(i1) + ", " + s + ")";
}