如何圆秒到十分之一?

时间:2014-04-05 23:04:05

标签: java

我认为通过将值转换为float它会显示到十分位置的秒数。

long startTime = System.currentTimeMillis();

    while (RUNNING) {
        track.repaint();
        // varying the x position movement
        horse.setX(xpos += (int) (Math.random() * 10 + 0));
        // Sleeping the thread
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (xpos >= FINISH_LINE) {              
            RUNNING = false;
            long endTime = System.currentTimeMillis();
            JOptionPane.showMessageDialog(new JFrame(), id + " Won and took " +(float)(startTime - endTime)/1000*-1.0+ " seconds");             
        }

    }
相反,它给了我太多的精确度。我想把它剪掉一点。

1 个答案:

答案 0 :(得分:1)

很容易!使用DecimalFormat

JOptionPane.showMessageDialog(new JFrame(), id + " Won and took " + new DecimalFormat(".##").format((float)(startTime - endTime)/1000*-1.0)+ " seconds");