How to convert a double to an int in Dart?

Dart

Dart Problem Overview


The following produces the below error:

int calc_ranks(ranks)
{
  double multiplier = .5;
  return multiplier * ranks;
}

The return type double is not a int, as defined by the method calc_ranks. How do I round/cast to an int?

Dart Solutions


Solution 1 - Dart

Round it using the round() method:

int calc_ranks(ranks) {
    double multiplier = .5;
    return (multiplier * ranks).round();
}

Solution 2 - Dart

You can use any of the following.

double d = 20.5;

int i = d.toInt(); // i = 20
int i = d.round(); // i = 21
int i = d.ceil();  // i = 21
int i = d.floor(); // i = 20

Solution 3 - Dart

You can simply use toInt() to convert a num to an int.

int calc_ranks(ranks)
{
  double multiplier = .5;
  return (multiplier * ranks).toInt();
}

Note that to do exactly the same thing you can use the Truncating division operator :

int calc_ranks(ranks) => ranks ~/ 2;

Solution 4 - Dart

I see a lot of answers, but with less description. Hope my answer will add some value. Lets initalize the variable, and see how it will change with different methods.

 double x = 8.5;

toInt()

It truncates the decimal value.

 int a = x.toInt();
 print(a); // 8

truncate()

It also truncates the decimal value.

 int b = x.truncate();
 print(b); // 8

round()

It returns the closest integer. It uses half up rounding mode.

  int c = x.round();
  print(c); // 9

ceil()

It returns the closest integer greater than the value.

  int c = x.ceil();
  print(c); // 9

floor()

It returns the closest integer smaller than the value.

  int c = x.floor();
  print(c); // 8

Solution 5 - Dart

Dart round double to int

Using round() method, we can get an integer closest to a double.

For example:

int num1 = (2.3).round();
// 2

int num2 = (2.5).round();
// 3

int num3 = (-2.3).round();
// -2

int num4 = (-2.5).round();
// -3

You can also try to those methods convert double to int in a Flutter

  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(e); // 2

Solution 6 - Dart

Its easy,

(20.8).round()

For String,

double.tryParse(20.8).round()

Solution 7 - Dart

from string to int ->

  1. if you string in int format like '10' then use ---->

    int.parse(value)

    but if string in double format like '10.6' then use like this ---->

    double.parse(value).toInt()

  2. convert double to int

    doubleValue.toInt()

Solution 8 - Dart

Try this!

int calc_ranks(ranks)
{
  double multiplier = .5;
  return (multiplier * ranks).truncate();
}

Solution 9 - Dart

There's another alternative, you can first cast the double to 'num' datatype and then convert to int using toInt().

  double multiplier = .5;
  return ((multiplier * ranks) as num).toInt();

The num type is an inherited data type of the int and double types. You can cast both int and double to num, then cast it again to whatever you want

(double -> use toDouble(), int -> use toInt())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTodd ChamberyView Question on Stackoverflow
Solution 1 - Dartuser1804599View Answer on Stackoverflow
Solution 2 - DartCopsOnRoadView Answer on Stackoverflow
Solution 3 - DartAlexandre ArdhuinView Answer on Stackoverflow
Solution 4 - DartJitesh MohiteView Answer on Stackoverflow
Solution 5 - DartParesh MangukiyaView Answer on Stackoverflow
Solution 6 - DartgsmView Answer on Stackoverflow
Solution 7 - DartRasel KhanView Answer on Stackoverflow
Solution 8 - DartLenView Answer on Stackoverflow
Solution 9 - DartSaravananView Answer on Stackoverflow