Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ActionScript (NaN==parseFloat(input.text)) warns that it will always be false. Why?

Despite the rather clear documentation which says that parseFloat() can return NaN as a value, when I write a block like:

if ( NaN == parseFloat(input.text) ) {
  errorMessage.text = "Please enter a number."
}

I am warned that the comparison will always be false. And testing shows the warning to be correct.

Where is the corrected documentation, and how can I write this to work with AS3?

like image 701
dlamblin Avatar asked Sep 29 '08 20:09

dlamblin


3 Answers

Because comparing anything to NaN is always false. Use isNaN() instead.

like image 78
Duncan Smart Avatar answered Sep 28 '22 02:09

Duncan Smart


isNaN(parseFloat(input.text))

like image 22
olle Avatar answered Sep 28 '22 03:09

olle


BTW, if for some reason you don't have access to isNaN(), the traditional method is to compare the number to itself:

if( number != number )
{
    //Is NaN 
}
like image 34
Matt W Avatar answered Sep 28 '22 01:09

Matt W