Davide Bicego
Davide Bicego

Reputation: 632

Why checking if double is NaN doesn't work in Dart?

Is there a working way to check if a double variable is NaN?

I tried:

variable.isNaN
variable == double.nan

Here the complete code:

bool isValidQuantity(String s) {
  double converted = toDouble(s);
  if (converted == double.nan || converted < 0) {
    return false;
  }
  return true;
}

toDouble() is a function of the library validators version 2.0.0+1

It just returns double.nan in this case so I don't get why false isn't returned by the isValidQuantity() function

Upvotes: 0

Views: 1482

Answers (1)

Ravinder Kumar
Ravinder Kumar

Reputation: 8010

It is working fine. However, you might want to initialize your variable because in dart everything is Object(any int, float, bool etc) and default values of the object are Null.

  var myVar = 0.0;
    if (myVar.isNaN)
      print('Not a number');
    else
      print('${myVar} is a number');

Upvotes: 1

Related Questions