java - How to round seconds to tenths place? -


this question has answer here:

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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -