how correctly display nanotime to second conversion
Asked Answered
N

2

6

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.

Need answered 2/11, 2013 at 17:14 Comment(2)
9.1611E-4 is a valid way of representing a number. It's just scientific notation.Their
There are a lot of questions on formatting doubles. You could go look them up.Kush
P
11

I think you need: DecimalFormat

System.out.println("solution Time : " + new DecimalFormat("#.##########").format(seconds) + " Seconds");
Phonate answered 2/11, 2013 at 17:48 Comment(1)
Thank you, i didn't know there was a method like this out there. Im going to spend a bit of time looking round the API now. Thanks all.Need
V
5
System.out.format("solution Time : %f Seconds", seconds);

for the classic, non-exponential floating point number.

Vulcanite answered 2/11, 2013 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.