Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for NaN in java

Tags:

java

android

I'm wondering if there is a way to test for NaN in java. The code below is returning NaN where it should be returning "NA".

if (tempAlloc == Double.NaN) {
                tv4.setText("NA");
            } else {
                tv4.setText(customFormat("###.#%",
                        Double.toString(tempAlloc)));
            }
like image 945
locoboy Avatar asked Mar 23 '11 08:03

locoboy


1 Answers

Usa Double.isNaN(tempAlloc). It returns true, when the argument is NaN and false otherwise.

This is implemented by checking if the argument is not equal to itself (a unique property of NaN values):

boolean isNaN == tempAlloc != tempAlloc;
like image 74
Joachim Sauer Avatar answered Oct 29 '22 09:10

Joachim Sauer