Reputation: 167
I have a Flutter project, in SharedPrefs, I'm storing a double id, from a Third-part API. (so I can only get it as a double)
The id from prefs is 43449716574226770.0
I have to parse this double to a String, I'm doing it like this:
String idString = id.toStringAsFixed(0)
What I'm getting is somehow: 43449716574226768
but I'd like it to be just 43449716574226770
I have tried .toInt().toString()
too, with the same results.
The weirdest part is that it sometimes works, with other ids.
Upvotes: 2
Views: 1107
Reputation: 109603
This number as long would be in a 6-byte range? As double this with an exponent part, this might mean that the previous double (bit wise) would have a difference more than 1. Also see java Math.ulp.
This implies that using a double
instead of a long
maps some double IDs to the same "long" ID.
I am not sure this is the problem here, but it would explain a deviation of 2.
Upvotes: 1