Eternalcode
Eternalcode

Reputation: 2412

What is the max value of integer in dart?

I have looked everywhere but I couldnt find any information related to this topic. Also, Is there a java - like Long / BigDecimal datatype in dart?

Upvotes: 15

Views: 17811

Answers (3)

Andrew
Andrew

Reputation: 38029

First option:

int maxInteger = double.maxFinite.toInt();

Better way:

const maxInteger = -1 >>> 1;

The >>> right shift with zero padding

will transform 1111 ... 1111 (-1 representation) to 0111 ... 1111 (max int representation)

const minInteger = -maxInteger;

Upvotes: 3

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657957

Dart 2

For dart2js generated JavaScript, Pixel Elephant's answer is still true.

within the range -253 to 253

Other execution platforms have fixed-size integers with 64 bits.

The type BigInt was added to typed_data

Since Dart 2.0 will switch to fixed-size integers, we also added a BigInt class to the typed_data library. Developers targeting dart2js can use this class as well. The BigInt class is not implementing num, and is completely independent of the num hierarchy.

Dart 1

There are also the packages

Upvotes: 15

Pixel Elephant
Pixel Elephant

Reputation: 21403

That depends if you are running in the Dart VM or compiling to JavaScript.

On the Dart VM an int is arbitrary precision and has no limit.

When compiling to JavaScript you are actually using floating point numbers, since that is all that JavaScript supports. This means you are restricted to representing integers within the range -253 to 253

Upvotes: 15

Related Questions