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(customForma开发者_JAVA百科t("###.#%",
Double.toString(tempAlloc)));
}
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;
精彩评论