13

I am collecting some data from a database and adding them together to get some statistics, but since I backdate some of my data then the calculated sum will sometime come up as NaN (not a number) I want to create an if sentence that says if(not a number) then exclude this data from my table.

How do I test if the data (in this case double) is NaN?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

3 Answers3

39

There are static methods Float.isNaN(float) and Double.isNaN(double) that you can use.

double x = ... // whatever calculation you do

if (Double.isNaN(x)) {
    ...
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
4

You can test for NaN two ways. You can use the built in function

Double.isNaN(x)

or perform the check this does which is

if (x != x)

provided x is a double or a float

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
-6

This would work for you.

if(number == Float.NaN)
Techie Manoj
  • 432
  • 3
  • 14