I want current date and time. so for that I am using "System.currentTimeMillis()" in java to get current date and time. Now I am converting milliseconds to seconds by dividing milliseconds to 1000. after dividing it I am converting this number into double, but I am getting number in this format "1.625147898E9", but I want this number in proper number format like this "34243893422.323". In the Double format. I searched for it, but did not find solution that can help. Please how can I do this please help.
Asked
Active
Viewed 182 times
0
-
2Does this answer your question? [How do I print a double value without scientific notation using Java?](https://stackoverflow.com/questions/16098046/how-do-i-print-a-double-value-without-scientific-notation-using-java) This may be of interest too: [Int division: Why is the result of 1/3 == 0?](https://stackoverflow.com/questions/4685450/int-division-why-is-the-result-of-1-3-0) – Ole V.V. Jul 01 '21 at 15:06
1 Answers
1
If you want your double
-value printed in another way, you can use either String.format
or System.out.printf
like this:
//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));
This will produce the following output:
Using printf: 1625205138,767000
Using String-Format %3f : 1625205138,767000
Using String-Format %.3f : 1625205138,767
Using String-Format %.0f : 1625205139
So if you desire 3 decimal places, use %.3f
to format your double
-value.

csalmhof
- 1,820
- 2
- 15
- 24
-
Bhai this is a good solution for my problem, but in this solution problem is that it do not have proper digit that should have after the point. In this all digit after the point is zero. – Shubham Mogarkar Jul 02 '21 at 04:47
-
1Yes, they are zero, because `System.currentTimeMillis()` returns a `long` and by dividing this directly by 1000 the digits after the point are cut. Anyway i edited my answer so the digits after the point will not be lost. – csalmhof Jul 02 '21 at 05:55
-
thanks brother now I got correct result that I was expecting to – Shubham Mogarkar Jul 03 '21 at 06:41
-
one problem in this is that it returns value as string, and when I convert it in double or float, then it again gets convert into scientific value like this "1.625295033888E9" – Shubham Mogarkar Jul 03 '21 at 06:55