简体   繁体   English

Java:要浮动的毫秒时间戳字符串

[英]Java: Millisecond timestamp string to float

I have this String: 1303317717.65384 - It's a UNIX timestamp (1303317717) with milliseconds (65384). 我有这个字符串:1303317717.65384 - 这是一个UNIX时间戳(1303317717),毫秒(65384)。

How can I convert this to a float in Java? 如何在Java中将其转换为浮点数? I am always getting 1.06172723E9 when giving it out, but I just want it to be 1303317717.65384. 放弃时我总是得到1.06172723E9,但我只想要它是1303317717.65384。

Thanks! 谢谢!

It is not possible to display this with enough precision one within a float variable - you have to use a double. 在float变量中不可能以足够的精度显示它 - 你必须使用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. Java中的浮点数只有大约六位数的精度。 You need a double. 你需要一个双倍。

If it's in the form of a String, then you can use Double.parseDouble(String s) . 如果它是String的形式,那么你可以使用Double.parseDouble(String s)

A float has insufficient precision. 浮子的精度不够。 Use a double instead. 请改用双。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM