Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Millisecond timestamp string to float

I have this String: 1303317717.65384 - It's a UNIX timestamp (1303317717) with milliseconds (65384).

How can I convert this to a float in Java? I am always getting 1.06172723E9 when giving it out, but I just want it to be 1303317717.65384.

Thanks!

like image 246
Lennart Koopmann Avatar asked Apr 20 '11 16:04

Lennart Koopmann


2 Answers

It is not possible to display this with enough precision one within a float variable - you have to use a double.

Demo:

System.out.println(String.format("%f", Float.parseFloat("1303317717.65384")));
System.out.println(String.format("%f", Double.parseDouble("1303317717.65384")));

yields

1303317760.000000
1303317717.653840
like image 152
Howard Avatar answered Sep 23 '22 06:09

Howard


Floats in Java only have about six digits of precision. You need a double.

If it's in the form of a String, then you can use Double.parseDouble(String s).

like image 41
jprete Avatar answered Sep 23 '22 06:09

jprete