how correctly display nanotime to second conversion

chris edwards picture chris edwards · Nov 2, 2013 · Viewed 21.3k times · Source

I have a BFS algorithm to solve the 8-Puzzle, and one of the project requirements is to output the amount of time it takes to find the shallowest solution.

I am using System.nanoTime() to keep track of the applications run time because it solves the majority of the given puzzles in well under a second.

The problem i am having is whem i convert my nanoTime to seconds, it displays in a weird format.

the following code is used:

final long startTime = System.nanoTime();

//algorithm code removed for simplicity this all functions correctly


final long duration = System.nanoTime() - startTime;
final double seconds = ((double)duration / 1000000000);
System.out.println("Nano time total: " + duration);
System.out.println("solution Time : " + seconds + " Seconds");

This produces the output:

 Nano time total: 916110
 solution time : 9.1611E-4 Seconds 

I have also tried using floats to represent the values.

is anybody able to provide a better way to convert/display, maybe use a format output statement?

Thanks for taking the time to read my question.

Answer

akasha picture akasha · Nov 2, 2013

I think you need: DecimalFormat

System.out.println("solution Time : " + new DecimalFormat("#.##########").format(seconds) + " Seconds");