I have a double in Java and I want to check if it is NaN.
What is the best way to do this?
I have a double in Java and I want to check if it is NaN.
What is the best way to do this?
If your value under test is a Double (not a primitive) and might be null (which is obviously not a number too), then you should use the following term:
(value==null || Double.isNaN(value))
Since isNaN() wants a primitive (rather than boxing any primitive double to a Double), passing a null value (which can't be unboxed to a Double) will result in an exception instead of the expected false.
The below code snippet will help evaluate primitive type holding NaN.
double dbl = Double.NaN;
Double.valueOf(dbl).isNaN() ? true : false;