當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Double isNaN()用法及代碼示例


Java Double類的isNaN()方法是Java中的內置方法,如果此Double值或指定的double值為Not-a-Number(NaN),則返回true,否則返回false。

用法

public boolean isNaN()
        or
public static boolean isNaN(double val)

參數:該函數接受單個參數val,該參數指定當直接使用Double類作為靜態方法調用時要檢查的值。當該方法用作實例方法時,不需要該參數。


返回值:如果val是NaN,則返回true;否則返回false。

以下程序說明了Java中的isNaN()方法:

示例1:

// Java code to demonstrate 
// Double isNaN() method 
// without parameter 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // first example 
        Double f1 = new Double(1.0 / 0.0); 
  
        boolean res = f1.isNaN(); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
  
        // second example 
        f1 = new Double(0.0 / 0.0); 
  
        res = f1.isNaN(); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
    } 
}
輸出:
Infinity is not NaN
NaN is NaN

示例2:

// Java code to demonstrate 
// Double isNaN() method 
// with parameter 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // first example 
        Double f1 = new Double(1.0 / 0.0); 
  
        boolean res = f1.isNaN(f1); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
  
        // second example 
        f1 = new Double(0.0 / 0.0); 
  
        res = f1.isNaN(f1); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
    } 
}
輸出:
Infinity is not NaN
NaN is NaN

參考: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#isNaN()



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Double isNaN() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。