开发者

Java: Millisecond timestamp string to float

开发者 https://www.devze.com 2023-02-28 04:21 出处:网络
I have this String: 1303317717.65384 - It\'s a UNIX timestamp (1303317717) with milliseconds (65384).

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!


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


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


A float has insufficient precision. Use a double instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号