{

How to do Integer division in dart | Flutter By Example


In Dart, the Division operator / is used to divide the numbers.

For example, two integer numbers are divided with the / operator, and the result is stored into an integer.

void main() {
  int first = 10;
  int second = 2;
  int result;

  result = first / second;
}

The above code throws compilation error Error: A value of type ‘double’ can’t be assigned to a variable of type ‘int’.

Int is a number without decimals, and double is a number with decimals.

Since the division operator always returns double values. if you assign the typed int variable with the result, It gives a compilation error.

There are a number of ways to fix this.

Dart number division examples

The first way, use truncating division operator(~/) operator to return the int type only.

/ operator always results in a double value. the truncating division operator(~/) results in int number by truncating double decimal values

Here is an example program

void main() {
  int first = 10;
  int second = 2;
  int result;

  result = first ~/ second;
  print(result);
  print(result.runtimeType);
}

Output:

5
int

Second-way using return result type as var or double with division operator.

In this example, use division operator to return of dynamic variable declared with var or double instead of int. the dynamic variable can be of any type such as int and double.

Here is an example program

void main() {
  int first = 10;
  int second = 2;
  int result;

  result = first ~/ second;
  print(result);
  print(result.runtimeType);
}

Output:

5.5
double

The third way is using num type instead of int.

int and double are a subclass of num. so use the result variable of type num Here is an example program

void main() {
  int first = 11;
  int second = 2;
  num result;

  result = first / second;
  print(result);
  print(result.runtimeType);
}

Output:

5.5
double

The fourth way, use floor or ceil to truncate the decimal part.

floor() method returns a lower int number, which is less than the result double value. ceil() method returns an upper int number that is greater than the result double value

void main() {
  int first = 11;
  int second = 2;
  int result;
  int result1;

  result = (first / second).floor();
  result1 = (first / second).ceil();

  print(result);
  print(result1);
}

Output

5
6

Conclusion

To summarize, Since integer division always returns double, assigning to int results a compilation error. Discussed multiple ways to avoid this

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example