Ben*_*n S 459
使用静态Double.isNaN(double)方法或您Double的.isNaN()方法.
// 1. static method
if (Double.isNaN(doubleValue)) {
...
}
// 2. object's method
if (doubleObject.isNaN()) {
...
}
Run Code Online (Sandbox Code Playgroud)
简单地做:
if (var == Double.NaN) {
...
}
Run Code Online (Sandbox Code Playgroud)
Grz*_*jos 12
您可能还想考虑检查值是否为有限值Double.isFinite(value).从Java 8开始,Double类中有一个新方法,您可以立即检查值是否为NaN和无穷大.
/**
* Returns {@code true} if the argument is a finite floating-point
* value; returns {@code false} otherwise (for NaN and infinity
* arguments).
*
* @param d the {@code double} value to be tested
* @return {@code true} if the argument is a finite
* floating-point value, {@code false} otherwise.
* @since 1.8
*/
public static boolean isFinite(double d)
Run Code Online (Sandbox Code Playgroud)
您可以使用检查NaN var != var.NaN不相等NaN.
编辑:这可能是迄今为止最糟糕的方法.这令人困惑,可读性和整体糟糕的做法都很糟糕.
| 归档时间: |
|
| 查看次数: |
174021 次 |
| 最近记录: |