147

Very simple issue. I have the useless class:

class Useless{
  double field;
  Useless(this.field);
}

I then commit the mortal sin and call new Useless(0); In checked mode (which is how I run my tests) that blows up, because 'int' is not a subtype of type 'double'.

Now, it works if I use new Useless(0.0) , but honestly I spend a lot of time correcting my tests putting .0s everywhere and I feel pretty dumb doing that.

As a temporary measure I rewrote the constructor as:

    class Useless{
      double field;
      Useless(num input){
          field = input.toDouble();
      }
    }

But that's ugly and I am afraid slow if called often. Is there a better way to do this?

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
CarrKnight
  • 2,768
  • 2
  • 23
  • 25
  • 8
    Why do you need a double? Can't you just use `num` everywhere? – Robert Oct 17 '14 at 04:39
  • 2
    Following the Dart style guide: "PREFER using double or int instead of num for parameter type annotations in performance sensitive code" – CarrKnight Oct 17 '14 at 04:52
  • 5
    This doesnt look like performance sensitive code to me. I think you should decide if you need a general number or if you really need int or double and then you should change all your code to be num, int or double. – Robert Oct 17 '14 at 06:50
  • 4
    @Robert Using `int` and `double` instead of `num` is better for conveying intention. Why stop at using `num`? We can just make all variables `dynamic` and get rid of all strong typing annoyances in one strike... – obe Sep 18 '21 at 14:10

9 Answers9

191

Simply toDouble()

Example:

int intVar = 5;
double doubleVar = intVar.toDouble();

Thanks to @jamesdlin who actually gave this answer in a comment to my previous answer...

Ronen Rabinovici
  • 8,680
  • 5
  • 34
  • 46
92

In Dart 2.1, integer literals may be directly used where double is expected. (See https://github.com/dart-lang/sdk/issues/34355.)

Note that this is syntactic sugar and applies only to literals. int variables still won't be automatically promoted to double, so code like:

double reciprocal(double d) => 1 / d;

int x = 42;
reciprocal(x);

would fail, and you'd need to do:

reciprocal(x.toDouble());
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
46

You can also use:

int x = 15;
double y = x + .0;
Mattia
  • 5,909
  • 3
  • 26
  • 41
18

use toDouble() method.
For e.g.:

int a = 10
print(a.toDouble) 
//or store value in a variable and then use
double convertedValue = a.toDouble()
Mangaldeep Pannu
  • 3,738
  • 2
  • 26
  • 45
6

From this attempt:

class Useless{
  double field;
  Useless(num input){
    field = input.toDouble();
  }
}

You can use the parse method of the double class which takes in a string.

class Useless{
  double field;
  Useless(num input){
    field = double.parse(input.toString()); //modified line
  }
}

A more compact way of writing the above class using constructor's initialisers is:

class Useless{
  double _field;
  Useless(double field):_field=double.parse(field.toString());
}
Lebohang Mbele
  • 3,321
  • 1
  • 15
  • 20
5

Since all divisions in flutter result to a double, the easiest thing I did to achieve this was just to divide the integer value with 1: i.e. int x = 15; double y = x /1;

Michael Elimu
  • 61
  • 1
  • 3
4

There's no better way to do this than the options you included :(

I get bitten by this lots too, for some reason I don't get any warnings in the editor and it just fails at runtime; mighty annoying :(

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
2

I'm using a combination:

static double checkDouble(dynamic value) {
  if (value is String) {
    return double.parse(value);
  } else if (value is int) {
    return 0.0 + value;
  } else {
    return value;
  }
}
Ricardo
  • 2,086
  • 25
  • 35
0

This is how you can cast from int to double int a = 2; double b = a*1.0;