Dart double toStringAsFixed rounds the number, but it shouldn't

Viewed 332

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.

3 Answers

enter image description hereCan you please try this

double x=43449716574226770.0;
print(x.toString());

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.

Make it id.round().toString(), it will work.

Related