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 void main(String[] args) {
    System.out.println(Math.round(10f));
    System.out.println(Math.round(2.5f));
    System.out.println(Math.round(-1.4f));
    System.out.println(Math.round(-2.5f));

}

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();//www  .j  a  v  a  2s .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:Main.java

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

    // find the closest long for these doubles
    System.out.println("Math.round(" + x + ")=" + Math.round(x));
    System.out.println("Math.round(" + y + ")=" + Math.round(y));

}

From source file:Main.java

public static void main(String[] args) {

    float x = 1234.1234f;
    float y = -1234.123f;

    // find the closest int for these floats
    System.out.println("Math.round(" + x + ")=" + Math.round(x));
    System.out.println("Math.round(" + y + ")=" + Math.round(y));

}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    list.add("E");
    list.add("F");
    list.add("G");
    list.add("H");

    System.out.println(list);/*from  ww w. j a  va 2  s .c om*/

    while (!list.isEmpty()) {
        long index = Math.round(Math.floor(Math.random() * list.size()));
        System.out.println("Name " + list.get((int) index));
        list.remove((int) index);
    }

}

From source file:Main.java

public static void main(String[] args) {
    final int width = 512;
    final int height = 512;
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics g = img.getGraphics();
    g.setColor(Color.black);/*from   w w w  . j  a  v a  2s  .  c  om*/
    g.fillRect(0, 0, width, height);
    g.setColor(Color.white);
    final double A = 8;
    final double B = 0.5;
    final double N = 4;
    final double scale = 128;
    final double zoom = 50;
    final double step = 1 / scale;
    Point last = null;
    final Point origin = new Point(width / 2, height / 2);

    for (double t = 0; t <= 2 * Math.PI; t += step) {
        final double r = zoom * polarFunction(t, A, B, N);
        final int x = (int) Math.round(r * Math.cos(t));
        final int y = (int) Math.round(r * Math.sin(t));
        Point next = new Point(x, y);
        if (last != null) {
            g.drawLine(origin.x + last.x, origin.y + last.y, origin.x + next.x, origin.y + next.y);
        }
        last = next;
    }

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from w  w  w  .  j  a va2s. com*/
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";

        ResultSet rs = stmt.executeQuery(sqlQuery);

        int rowSize = 0;
        while (rs.next()) {
            rowSize++;
        }

        System.out.println("Number of Rows in ResultSet is: " + rowSize);
        if (rowSize == 0) {
            System.out.println("Since there are no rows, exiting...");
            System.exit(0);
        }

        int cursorPosition = Math.round(rowSize / 2);

        System.out.println("Moving to position: " + cursorPosition);
        rs.absolute(cursorPosition);
        System.out.println("Name: " + rs.getString(2));

        rs.relative(-1);

        cursorPosition = rs.getRow();
        System.out.println("Moving to position: " + cursorPosition);
        System.out.println("Name: " + rs.getString(2));

        System.out.println("Moving to the first row");
        while (!rs.isFirst()) {
            rs.previous();
        }
        System.out.println("Name: " + rs.getString(2));
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:org.eclipse.swt.snippets.Snippet355.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Snippet 355");
    final Image image = display.getSystemImage(SWT.ICON_QUESTION);
    shell.addListener(SWT.Paint, e -> {
        Rectangle rect = image.getBounds();
        int width = rect.width;
        int height = rect.height;
        GC gc = e.gc;//from www . j  a  v  a 2  s.c  om
        int x = 10, y = 10;
        gc.drawImage(image, 0, 0, width, height, x, y, width, height);
        gc.drawImage(image, 0, 0, width, height, x + width, y, (int) Math.round(width * 0.5),
                (int) Math.round(height * 0.5));
        gc.drawImage(image, 0, 0, width, height, x + width + (int) Math.round(width * 0.5), y, width * 2,
                height * 2);
    });
    shell.setSize(600, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:edu.cuhk.hccl.SequenceFileWriter.java

public static void main(String[] args) throws IOException {
    // Check parameters
    if (args.length < 3) {
        System.out.print("Please specify three parameters!");
        System.exit(-1);/*from w  ww.j  av a2  s .  com*/
    }

    File filesDir = new File(args[0]);
    String targetDir = args[1];
    final int NUM_SPLITS = Integer.parseInt(args[2]);

    // Remove old target directory first
    FileUtils.deleteQuietly(new File(targetDir));

    File[] dataFiles = filesDir.listFiles();

    int total = dataFiles.length;
    int range = (int) Math.round(total / NUM_SPLITS + 0.5);
    System.out.printf("[INFO] The number of total files is %d \n.", total);

    for (int i = 1; i <= NUM_SPLITS; i++) {
        int start = (i - 1) * range;
        int end = Math.min(start + range, total);
        File[] subFiles = Arrays.copyOfRange(dataFiles, start, end);
        createSeqFile(subFiles, FilenameUtils.normalize(targetDir + "/" + i + ".seq"));
    }

    System.out.println("[INFO] All files have been successfully processed!");
}

From source file:cat.tv3.eng.rec.recomana.lupa.visualization.RecommendationToJson.java

public static void main(String[] args) throws IOException {
    final int TOTAL_WORDS = 20;
    String host = args[0];//from  www . j  a v a2 s .  c  o  m
    int port = Integer.parseInt(args[1]);
    Jedis jedis = new Jedis(host, port, 20000);

    String[] recommendation_keys = jedis.keys("recommendations_*").toArray(new String[0]);

    for (int i = 0; i < recommendation_keys.length; ++i) {
        JSONArray recommendations = new JSONArray();
        String[] split_reco_name = recommendation_keys[i].split("_");
        String id = split_reco_name[split_reco_name.length - 1];

        Set<Tuple> recos = jedis.zrangeWithScores(recommendation_keys[i], 0, -1);
        Iterator<Tuple> it = recos.iterator();

        while (it.hasNext()) {
            Tuple t = it.next();

            JSONObject new_reco = new JSONObject();
            new_reco.put("id", t.getElement());
            new_reco.put("distance", (double) Math.round(t.getScore() * 10000) / 10000);

            recommendations.add(new_reco);
        }
        saveResults(recommendations, id);
    }
    jedis.disconnect();
}