• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help people that has worked a a treat.
john
reply
    Bookmark Topic Watch Topic
  • New Topic