java - How to round seconds to tenths place? -
this question has answer here:
- java decimal string format 6 answers
i thought casting value float show seconds tenths place.
long starttime = system.currenttimemillis(); while (running) { track.repaint(); // varying x position movement horse.setx(xpos += (int) (math.random() * 10 + 0)); // sleeping 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 , took " +(float)(starttime - endtime)/1000*-1.0+ " seconds"); } }
instead, giving me way precision. cut off bit.
it's easy! use decimalformat
:
joptionpane.showmessagedialog(new jframe(), id + " won , took " + new decimalformat(".##").format((float)(starttime - endtime)/1000*-1.0)+ " seconds");
Comments
Post a Comment