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

Tags: float

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

How to check a float value is Not-a-Number value in Java

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

CheckFloatNaNExample.java

public class CheckFloatNaNExample {
    public static void main(String... args) {
        float floatValue1 =  0.0f / 0.0f;
        float floatValue2 = 99.88f;

        // Check Float is Not-a-Number
        boolean result1 = Float.isNaN(floatValue1);
        boolean result2 = Float.isNaN(floatValue2);

        System.out.println("floatValue1 : " + floatValue1);
        System.out.println("floatValue1 is not a number : " + result1);
        
        System.out.println("floatValue2 : " + floatValue2);
        System.out.println("floatValue2 is not a number : " + result2);
    }
}
The output as below.
floatValue1 : NaN
floatValue1 is not a number : true
floatValue2 : 99.88
floatValue2 is not a number : false

Happy Coding 😊

Java Check Float is Infinite Number

Java Check Float is Finite Number