• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people
Hope maybe someone could figure this one out.
I am trying to write some code that displays the time in seconds to millisecond precision i.e. 1.354. Unfortnatley my code at the moment displays 1.354 seconds as 1354. Could someone let me know how to cast this correctly i have included my code below.
public long dt = 0;
public long startTime = System.currentTimeMillis();
dt = System.currentTimeMillis() - startTime;
print(dt);
thanks for your time
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The time difference you measure will be in milliseconds; hence the name "currentTimeMillis()". If you want the time in seconds, then divide by 1000:
print(dt/1000.0);
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
In order to have the print only show three values after the decimal use a DecimalFormat object:
DecimalFormat df = new DecimalFormat( "#0.000" );
System.out.println(df.format(value));

Regards,
Manfred.
 
john omeara
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help people that has worked a a treat.
john
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!