我有一个 BFS 算法来解决 8-Puzzle,项目要求之一是输出找到最浅解所需的时间。
我正在使用 System.nanoTime()
来跟踪应用程序的运行时间,因为它可以在不到一秒的时间内解决大部分给定的难题。
我遇到的问题是当我将 nanoTime
转换为秒时,它以一种奇怪的格式显示。
使用了以下代码:
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");
这会产生输出:
Nano time total: 916110
solution time : 9.1611E-4 Seconds
我也尝试过使用 float 来表示值。
有没有人能提供更好的转换/显示方式,也许使用格式输出语句?
感谢您花时间阅读我的问题。
最佳答案
我想你需要:DecimalFormat
System.out.println("solution Time : " + new DecimalFormat("#.##########").format(seconds) + " Seconds");
关于java - 如何正确显示纳秒到秒的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19744586/