[英]Data type for nanosecond time precision in Java
I would like to have a numeric variable that contains integer number with nanosecond precision.我想要一个数字变量,其中包含具有纳秒精度的 integer 数字。 I tried this:
我试过这个:
Instant t = Instant.now();
a = BigDecimal.valueof(t.getEpochSecond() * 1e9 + t.getNano());
b = BigDecimal.valueof(t.getEpochSecond() * 1e9 + t.getNano() + 1);
Both a
and b
are contains same value (eg 1.60681511777265408E+18), the +1 operation is lost here. a
和b
都包含相同的值(例如 1.60681511777265408E+18), +1操作在这里丢失。
Any idea how to deal with this precision?知道如何处理这种精度吗? The goal of it is to keep nanosecond-precision timestamp for time column in InfluxDB.
它的目标是为 InfluxDB 中的时间列保留纳秒精度的时间戳。 I understand it has something to do with double precision ( Java BigDecimal difference ), but I haven't found any working solution yet.
我知道它与双精度有关( Java BigDecimal 差异),但我还没有找到任何可行的解决方案。
Just do the operations with the methods in BigDecimal
.只需使用
BigDecimal
中的方法进行操作。 And make sure you don't use valueOf(double)
.并确保您不使用
valueOf(double)
。 You can use valueOf(long)
, however.但是,您可以使用
valueOf(long)
。
BigDecimal a = BigDecimal.valueOf(t.getEpochSecond())
.multiply(BigDecimal.valueOf(1_000_000_000))
.add(BigDecimal.valueOf(t.getNano()));
BigDecimal b = BigDecimal.valueOf(t.getEpochSecond())
.multiply(BigDecimal.valueOf(1_000_000_000))
.add(BigDecimal.valueOf(t.getNano()))
.add(BigDecimal.ONE);
Printing a
and b
gives something like:打印
a
和b
给出如下内容:
1606815981554921000
1606815981554921001
If you're storing an integer value, why use a BigDecimal
instead of a BigInteger
?如果您要存储 integer 值,为什么要使用
BigDecimal
而不是BigInteger
?
import java.time.*;
import java.math.*;
public class MyClass {
public static void main(String args[]) {
Instant t = Instant.now();
BigInteger a = BigInteger.valueOf(t.getEpochSecond() * 1_000_000_000
+ t.getNano());
BigInteger b = BigInteger.valueOf(t.getEpochSecond() * 1_000_000_000
+ t.getNano() + 1);
System.out.println(a); // 1606816120696314000
System.out.println(b); // 1606816120696314001
}
}
Need to switch to BigInteger
math methods in the year 2262 though, because the long
argument will start overflowing.不过需要在 2262 年切换到
BigInteger
数学方法,因为long
参数将开始溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.