How to Tell If a Float Number Is NaN in Java


Here we show how to tell if a float number is NaN using isNaN() method of the Float class in Java.

Source Code

package com.beginner.examples;

public class FloatIsNaNExample {

	public static void main(String[] args) {

		Float f = new Float(Math.sqrt(-4));
		
		// Use isNaN() in the Float object to determine if it is valid
		if (f.isNaN()) {
			
			System.out.println("This number is NaN");
		}

	}

}

Output:

This number is NaN
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments