gpt4 book ai didi

java - 如何将秒四舍五入到十分位?

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:53 24 4
gpt4 key购买 nike

我认为通过将值转换为 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");
}

}

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

最佳答案

很简单!使用DecimalFormat:

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

关于java - 如何将秒四舍五入到十分位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22888008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com