Máté Antal
Máté Antal

Reputation: 167

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

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

Answers (3)

Joop Eggen
Joop Eggen

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

Salim Murshed
Salim Murshed

Reputation: 1451

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

Upvotes: 0

Mofidul Islam
Mofidul Islam

Reputation: 520

enter image description hereCan you please try this

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

Upvotes: 1

Related Questions