Example usage for java.lang Math rint

List of usage examples for java.lang Math rint

Introduction

In this page you can find the example usage for java.lang Math rint.

Prototype

public static double rint(double a) 

Source Link

Document

Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

Usage

From source file:Main.java

public static void main(String[] args) {
    double x = 123456.7;
    double y = -123.45;

    // find the closest integers for these double numbers
    System.out.println("Math.rint(" + x + ")=" + Math.rint(x));
    System.out.println("Math.rint(" + y + ")=" + Math.rint(y));

}

From source file:MainCLass.java

public static void main(String[] args) {
    double x = 2.4;
    double y = 9.5;
    double z = -1.3;

    System.out.println("round(x) = " + Math.round(x));
    System.out.println("round(y) = " + Math.round(y));
    System.out.println("round(z) = " + Math.round(z));
    System.out.println();//  w w  w  . ja  v  a 2  s .co  m
    System.out.println("ceil(x) = " + Math.ceil(x));
    System.out.println("ceil(y) = " + Math.ceil(y));
    System.out.println("ceil(z) = " + Math.ceil(z));
    System.out.println();
    System.out.println("floor(x) = " + Math.floor(x));
    System.out.println("floor(y) = " + Math.floor(y));
    System.out.println("floor(z) = " + Math.floor(z));
    System.out.println();
    System.out.println("rint(x) = " + Math.rint(x));
    System.out.println("rint(y) = " + Math.rint(y));
    System.out.println("rint(z) = " + Math.rint(z));
}

From source file:org.jfree.chart.demo.LegendTitleToImageDemo1.java

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("A", 1.0D);
    defaultpiedataset.setValue("B", 2D);
    defaultpiedataset.setValue("C", 3D);
    JFreeChart jfreechart = ChartFactory.createPieChart("Test", defaultpiedataset, true, false, false);
    LegendTitle legendtitle = jfreechart.getLegend();
    legendtitle.setMargin(0.0D, 0.0D, 1.0D, 1.0D);
    BufferedImage bufferedimage = new BufferedImage(1, 1, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    Size2D size2d = legendtitle.arrange(graphics2d);
    graphics2d.dispose();//from   w ww.j  a v  a 2 s .c  om
    int i = (int) Math.rint(size2d.width);
    int j = (int) Math.rint(size2d.height);
    BufferedImage bufferedimage1 = new BufferedImage(i, j, 2);
    Graphics2D graphics2d1 = bufferedimage1.createGraphics();
    legendtitle.draw(graphics2d1, new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j));
    graphics2d1.dispose();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
            new FileOutputStream(new File("LegendTitleToImageDemo1.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

From source file:org.jfree.chart.demo.LegendTitleToImageDemo2.java

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("England", 1.0D);
    defaultpiedataset.setValue("France", 2D);
    defaultpiedataset.setValue("Germany", 3D);
    defaultpiedataset.setValue("Italy", 4D);
    defaultpiedataset.setValue("Scotland", 5D);
    defaultpiedataset.setValue("Belgium", 6D);
    defaultpiedataset.setValue("Poland", 7D);
    defaultpiedataset.setValue("Spain", 8D);
    defaultpiedataset.setValue("Portugal", 9D);
    defaultpiedataset.setValue("Switzerland", 10D);
    defaultpiedataset.setValue("Austria", 11D);
    defaultpiedataset.setValue("Luxembourg", 12D);
    JFreeChart jfreechart = ChartFactory.createPieChart("Test", defaultpiedataset, true, false, false);
    LegendTitle legendtitle = jfreechart.getLegend();
    legendtitle.setMargin(0.0D, 0.0D, 1.0D, 1.0D);
    BufferedImage bufferedimage = new BufferedImage(1, 1, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    Size2D size2d = legendtitle.arrange(graphics2d, new RectangleConstraint(250D, new Range(0.0D, 10000D)));
    graphics2d.dispose();/*ww w . ja v  a 2s.c o m*/
    int i = (int) Math.rint(size2d.width);
    int j = (int) Math.rint(size2d.height);
    BufferedImage bufferedimage1 = new BufferedImage(i, j, 2);
    Graphics2D graphics2d1 = bufferedimage1.createGraphics();
    legendtitle.draw(graphics2d1, new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j));
    graphics2d1.dispose();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
            new FileOutputStream(new File("LegendTitleToImageDemo2.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

From source file:BasicMathDemo.java

public static void main(String[] args) {
    double aNumber = -191.635;

    System.out.println("The absolute value of " + aNumber + " is " + Math.abs(aNumber));
    System.out.println("The ceiling of " + aNumber + " is " + Math.ceil(aNumber));
    System.out.println("The floor of " + aNumber + " is " + Math.floor(aNumber));
    System.out.println("The rint of " + aNumber + " is " + Math.rint(aNumber));
}

From source file:Main.java

public static int getStopY(float angle, int distance) {
    return Double.valueOf(Math.rint(-1 * distance * Math.sin(Math.toRadians(angle)))).intValue();
}

From source file:Main.java

public static int[] shuffle(int[] arr) {
    for (int i = arr.length - 1; i >= 0; --i) {
        // swap indexes
        int r = (int) Math.rint(Math.random() * i);
        int t = arr[i];
        arr[i] = arr[r];//from   w  w w. j a  v a2 s. c o  m
        arr[r] = t;
    }

    return arr;
}

From source file:Main.java

public static double getScale(float targetWidth, float targetHeight, float bmpWidth, float bmpHeight) {
    double be;// ww w.j  a  v a 2 s .com
    if (bmpWidth >= bmpHeight) {
        float widthScale = bmpWidth / targetHeight;
        float heightScale = bmpHeight / targetWidth;
        if (widthScale >= heightScale) {
            be = Math.rint(widthScale);
        } else {
            be = Math.rint(heightScale);
        }
    } else {
        float widthScale = bmpWidth / targetWidth;
        float heightScale = bmpHeight / targetHeight;
        if (widthScale >= heightScale) {
            be = widthScale;
        } else {
            be = heightScale;
        }
    }
    if (be <= 0) {
        return 1.0;
    }

    return be;
}

From source file:com.espertech.esper.support.util.DoubleValueAssertionUtil.java

public static boolean equals(double valueActual, double valueExpected, int precision) {
    if (precision < 1) {
        throw new IllegalArgumentException("Invalid precision value of " + precision + " supplied");
    }/*www . j  av  a  2 s .  c om*/

    if ((Double.valueOf(valueActual).isNaN()) && (Double.valueOf(valueExpected).isNaN())) {
        return true;
    }
    if (((Double.valueOf(valueActual).isNaN()) && (!Double.valueOf(valueExpected).isNaN()))
            || ((!Double.valueOf(valueActual).isNaN()) && (Double.valueOf(valueExpected).isNaN()))) {
        log.debug(".equals Compare failed, " + "  valueActual=" + valueActual + "  valueExpected="
                + valueExpected);
        return false;
    }

    double factor = Math.pow(10, precision);
    double val1 = valueActual * factor;
    double val2 = valueExpected * factor;

    // Round to closest integer
    double d1 = Math.rint(val1);
    double d2 = Math.rint(val2);

    if (d1 != d2) {
        log.debug(".equals Compare failed, " + "  valueActual=" + valueActual + "  valueExpected="
                + valueExpected + "  precision=" + precision);
        return false;
    }

    return true;
}

From source file:Main.java

/**
 * Truncate a double-precision floating point number to a specific number of significant digits
 *
 * @return The new truncated double number (make sure to catch it)
 *//*ww  w.java2 s . com*/
private static double truncateDouble(double inputDouble, int numberSignificantDigits) {
    double returnDouble = inputDouble * Math.pow(10, numberSignificantDigits);
    returnDouble = Math.rint(returnDouble);
    return (returnDouble / Math.pow(10, numberSignificantDigits));
}