views:

1638

answers:

4

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?

+14  A: 

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

Duncan Smart
So even: if (NaN==NaN) { /* unreachable */ }
dlamblin
+3  A: 

isNaN(parseFloat(input.text))

olle
A: 

Documentation can be found in the Adobe Flex Language Reference Here as well as other globally available functions.

JustFoo
+1  A: 

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 
}
Matt W