2

I am using milliseconds in my java program and converting it to seconds. After my method does this, it returns seconds in a long format.

System.out.println("[" + threadID + "]" + " " + "SeqSum res=" + grandTotal + " secs=" + stopTime);

I have used the variable stopTime to display the seconds.

My output is currently secs=0 It needs to be secs=0.000

I need it to be displayed to 3 decimal places using system.out.println() or system.out.format()

How can I reword my print statement so that I get the output secs=0.000?

Please help

WoLv3rine
  • 107
  • 1
  • 4
  • 9
  • What type is your `stopTime` variable? We need some more details. – Rob I May 15 '12 at 03:37
  • My stopTime variable was a long but I just changed it to a float so that I can use implement the format to a float to print in 3 d.ps – WoLv3rine May 15 '12 at 03:56

1 Answers1

8

Milliseconds are a long, and don't have decimal places. You will need to divide them by 1000.0f to get them into fractional seconds. Then you will need to use the format method to limit them to 3 decimal places.

Something like:

   final long ms;
   final float sec;

   ms = System.currentTimeMillis();
   sec = ms / 1000.0f;
   System.out.format("%.3f", sec);
TofuBeer
  • 60,850
  • 18
  • 118
  • 163