In Dart is there a quick way to convert int to double?

Dart

Dart Problem Overview


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?

Dart Solutions


Solution 1 - Dart

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...

Solution 2 - Dart

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

Solution 3 - Dart

You can also use:

int x = 15;
double y = x + .0;

Solution 4 - Dart

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

Solution 5 - Dart

The Dart language has two numeric types: int and double. The former is an arbitrary-precision signed integer, and the latter is the IEEE-754 double-precision floating-point number. 

There is no implicit conversion so you have to use toDouble explicitly, or maybe better: type y as num which is the superclass of double and int.

int x = 11;

double y = x.toDouble();

If the number is not representable as a double, an approximation is returned. For numerically large integers, the approximation may be infinite.

Solution 6 - Dart

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

Solution 7 - Dart

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

Solution 8 - Dart

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;

Solution 9 - Dart

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;
  }
}

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
QuestionCarrKnightView Question on Stackoverflow
Solution 1 - DartRonen RabinoviciView Answer on Stackoverflow
Solution 2 - DartjamesdlinView Answer on Stackoverflow
Solution 3 - DartMattiaView Answer on Stackoverflow
Solution 4 - DartUmar CalcuttawalaView Answer on Stackoverflow
Solution 5 - DartParesh MangukiyaView Answer on Stackoverflow
Solution 6 - DartLebohang MbeleView Answer on Stackoverflow
Solution 7 - DartDanny TuppenyView Answer on Stackoverflow
Solution 8 - DartMichael ElimuView Answer on Stackoverflow
Solution 9 - DartRicardoView Answer on Stackoverflow