Dart - Convert Double to Int And Vice Versa

This tutorial shows you how to perform conversion between double and int in Dart.

Convert double to int

There are some methods for converting double to int: toInt(), round(),ceil(), and floor(). All of those methods return int.

  • toInt() and truncate() work by truncating the decimal value.
  • round() returns the closest integer. It uses half up rounding mode.
  • ceil() returns the closest integer greater than the value.
  • floor() returns the closest integer smaller than the value.

Example:

  double x = 2.5;

  int a = x.toInt();
  int b = x.truncate();
  int c = x.round();
  int d = x.ceil();
  int e = x.floor();

  print(a); // 2
  print(b); // 2
  print(c); // 3
  print(d); // 3
  print(3); // 2

There are also truncateToDouble, roundToDouble, ceilToDouble, and floorToDouble variants which do the similar thing to their respective non-ToDouble variant, but they return double with one decimal point (.0).

Convert int to double

To convert an int to a double, use toDouble() method. The result is an integer with one decimal point (.0).

Example:

  int x = 10;

  double a = x.toDouble();

  print(a); # 10.0

The result is a double with one decimal point (.0).

That's how to convert between double and int in Dart. You may also be interested to read about how to round double to String with a certain decimal precision.