I would like to have a numeric variable that contains integer number with nanosecond precision. 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 (e.g. 1.60681511777265408E+18), the +1 operation is lost here.
Any idea how to deal with this precision? The goal of it is to keep nanosecond-precision timestamp for time column in InfluxDB. I understand it has something to do with double precision (Java BigDecimal difference), but I haven't found any working solution yet.
Just do the operations with the methods in
BigDecimal
. And make sure you don't usevalueOf(double)
. You can usevalueOf(long)
, however.Printing
a
andb
gives something like: