Java Data Type How to - Check if a double value is Infinite() and not-a-number with isNaN()








Question

We would like to know how to check if a double value is Infinite() and not-a-number with isNaN().

Answer

 //  w  ww. j a  v a  2  s . c  om

public class Main {
  public static void main(String args[]) {
    Double d1 = new Double(1/0.);
    Double d2 = new Double(0/0.);
   
    System.out.println(d1 + ": " + d1.isInfinite() + ", " + d1.isNaN());
    System.out.println(d2 + ": " + d2.isInfinite() + ", " + d2.isNaN());
  }
}

The code above generates the following result.