In Dart is there a quick way to convert int to double?
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());
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...
You can also use:
int x = 15;
double y = x + .0;