Java 如何将秒数四舍五入到十分之一?
我认为通过将值转换为float,它将显示秒到十分之一的位置Java 如何将秒数四舍五入到十分之一?,java,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
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");
}
}
相反,它给了我太多的精确性。我想把它剪掉一点。很简单!使用
DecimalFormat
:
JOptionPane.showMessageDialog(new JFrame(), id + " Won and took " + new DecimalFormat(".##").format((float)(startTime - endTime)/1000*-1.0)+ " seconds");