0

我想要当前的日期和时间。所以为此我在java中使用“System.currentTimeMillis()”来获取当前日期和时间。现在我通过将毫秒除以 1000 将毫秒转换为秒。除此之后,我将此数字转换为双精度数,但我得到的数字格式为“1.625147898E9”,但我希望这个数字采用正确的数字格式,如“34243893422.323” ”。在双格式。我搜索了它,但没有找到可以提供帮助的解决方案。请问我该怎么做,请帮忙。

4

1 回答 1

1

如果您希望您的double-value 以另一种方式打印,您可以使用String.formatSystem.out.printf像这样使用:

//save to double before dividing, to avoid digits after point to be lost
double milliseconds = System.currentTimeMillis();
double seconds = milliseconds / 1000;

System.out.printf("Using printf: %f \n", seconds);
System.out.println("Using String-Format %3f : " + String.format("%3f", seconds));
System.out.println("Using String-Format %.3f : " + String.format("%.3f", seconds));
System.out.println("Using String-Format %.0f : " + String.format("%.0f", seconds));

这将产生以下输出:

Using printf: 1625205138,767000 
Using String-Format %3f : 1625205138,767000
Using String-Format %.3f : 1625205138,767
Using String-Format %.0f : 1625205139

因此,如果您需要 3 位小数,请使用%.3f来格式化您的double-value。

于 2021-07-01T16:59:38.153 回答