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
  • 386,424
  • 207
  • 554
  • 861
Marc Rasmussen
  • 18,255
  • 71
  • 189
  • 330

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
  • 386,424
  • 207
  • 554
  • 861
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
  • 513,304
  • 74
  • 731
  • 1,106
-6

This would work for you.

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