Java Check Double is Not-a-Number (NaN)

Tags: double

In this Java core tutorial we learn how to check whether a double value is not-a-number (NaN) value or not in Java programming language.

How to check double value is Not-a-Number (NaN)

In Java, with a given double value we can use the Double.isNaN(double v) static method to check whether it is a Not-a-Number (NaN) value or not as the example Java code below.

CheckDoubleNaNExample.java

public class CheckDoubleNaNExample {
    public static void main(String... args) {
        double doubleValue1 =  0.0 / 0.0;
        double doubleValue2 = 12.34;

        // Check Double is Not-a-Number
        boolean result1 = Double.isNaN(doubleValue1);
        boolean result2 = Double.isNaN(doubleValue2);

        System.out.println("doubleValue1 : " + doubleValue1);
        System.out.println("doubleValue1 is not a number : " + result1);

        System.out.println("doubleValue2 : " + doubleValue2);
        System.out.println("doubleValue2 is not a number : " + result2);
    }
}
The output as below.
doubleValue1 : NaN
doubleValue1 is not a number : true
doubleValue2 : 12.34
doubleValue2 is not a number : false

Happy Coding 😊

Java Convert Double to Hexadecimal String

Java Check Double is Infinite Number

Java Check Double is Finite Number

Java Convert Double to BigInteger