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

Time:10-12

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.

CodePudding user response:

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 enter image description hereCan you please try this

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

CodePudding user response:

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

  • Related