且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:Java代码输出“NaN”

更新时间:2023-12-03 23:14:22

NaN 代表不是数字,是您尝试计算无效计算时使用的浮点占位符值。

NaN stands for Not a Number and is a floating point placeholder value that is used when you try to calculate an invalid computation.

最常见的此类操作是除以零。由于我在你的代码中只看到一个分区( ratio [i] = dollarValue [i] /盎司[i]; ),我猜是盎司[i] 此时 0

The most common such operation is division by zero. Since I see exactly one division in your code (ratio[i]=dollarValue[i]/ounce[i];), I'd guess that ounce[i] is 0 at that point.

请注意,你可以使用 == 检查 NaN ,因为 NaN 不等于任何值(甚至不是自己!)。

Note that you can't check for NaN using ==, because NaN is not equal to any value (not even itself!).

检查是否 float / double NaN 使用 Float.isNaN() Double.isNaN()

To check if a float/double is NaN use Float.isNaN() and Double.isNaN() respectively.