Lennart Koopmann
Lennart Koopmann

Reputation: 836

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!

Upvotes: 5

Views: 8241

Answers (3)

Richard H
Richard H

Reputation: 39125

A float has insufficient precision. Use a double instead.

Upvotes: 0

jprete
jprete

Reputation: 3759

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).

Upvotes: 2

Howard
Howard

Reputation: 39217

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

Upvotes: 10

Related Questions