208

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?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Todd Chambery
  • 3,195
  • 4
  • 19
  • 27

11 Answers11

257

Round it using the round() method:

int calc_ranks(ranks) {
    double multiplier = .5;
    return (multiplier * ranks).round();
}
  • 23
    Or truncate it to int: return (multiplier * ranks).toInt(); (see https://api.dartlang.org/docs/channels/stable/latest/dart_core/double.html) – Everton Dec 26 '13 at 17:22
  • 8
    @Evertion I had cases where `(0.57 * 100).toInt()` resolved to `56`, so guys, be careful. – 最白目 Nov 22 '19 at 10:20
175

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
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • 2
    So, what is the difference between toInt and floor or round and ceil – Hector Aguero Nov 11 '20 at 20:49
  • 3
    Floor and ceil would convert the double to closest int, going down and up respectively, so (20.1).floor() would be 20, (20.1).ceil() would be 21. toInt cuts off the mantissa, leaving just the characteristic, and round makes it so the numbers with mantissa below 0.5 go down, others go up. – Kamil Poniewierski Aug 03 '21 at 15:18
92

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;
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
23

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

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
5

I looked at the answers above and added some more answers to make it a little easier to understand.

double value = 10.5;

Using toInt()

void main(){
  double value = 10.5;
  var y = value.toInt();
  print(y);
  print(y.runtimeType);
}

Using round()

The round() method returns the closest integer to the double.

void main(){
  double value = 9.6;
  var b = value.round();
  print(b);
  print(b.runtimeType);
}

Using ceil()

The ceil() method returns the smallest integer that is equal or greater than the given double.

void main(){
  double value = 9.5;
  var d = value.ceil();
  print(d);
  print(d.runtimeType);
}

Using floor()

The floor() method returns the greatest integer not greater than the given double.

void main(){
  double value = 10.9;
  var j = value.floor();
  print(j);
  print(j.runtimeType);
}

Conclusion

We’ve gone through 4 different techniques to convert a double to an integer in Dart and Flutter. You can choose from the method that fits your use case to solve your problem. Flutter is awesome and provides a lot of amazing features.

4

To convert double to int just this: division

double01 ~/ double02

PS: The operator x ~/ y is more efficient than (x / y).toInt().

Multiplication, addition and subtraction

(double01 * double02).toInt
(double01 + double02).toInt
(double01 - double02).toInt
André
  • 71
  • 2
2

Its easy,

(20.8).round()

For String,

double.tryParse(20.8).round()
gsm
  • 2,348
  • 17
  • 16
1

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()

Rasel Khan
  • 2,976
  • 19
  • 25
0

Try this!

int calc_ranks(ranks)
{
  double multiplier = .5;
  return (multiplier * ranks).truncate();
}
Len
  • 3
  • 1
0
    class CurrencyUtils{
    
      static int doubletoint(double doublee) {
        double multiplier = .5;
        return (multiplier * doublee).round();
      }
    }
    
    ----------------------
    
    
    CustomText( CurrencyUtils.doubletoint(
double.parse(projPageListss[0].budget.toString())
).toString(),
         fontSize: 20,
         color: Colors.white,
         font: Font.QuicksandSemiBold,
                                            ), 
btm me
  • 368
  • 3
  • 11
-1

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())

Saravanan
  • 566
  • 5
  • 13