0

我遇到了一个奇怪的不Number想要的NaN转换,不知道在哪里以及为什么。我显然在这里遗漏了一些东西,但我不知道是什么。

任何建议都是最受欢迎的!;)

这是javascript代码:

updatedAbsoluteResult = currentlyDisplayedRentability * investment
resultAgainstReferencial = updatedAbsoluteResult - appropriateReferencialRentability * investment

$('#a0').html('currentlyDisplayedRentability: ' + typeof currentlyDisplayedRentability)
$('#a1').html('investment: ' + typeof investment)
$('#a2').html('updatedAbsoluteResult: ' + typeof updatedAbsoluteResult)
$('#a3').html('appropriateReferencialRentability: ' + typeof appropriateReferencialRentability)
$('#a4').html('resultAgainstReferencial: ' + typeof resultAgainstReferencial )
$('#a5').html('resultAgainstReferencial: ' + resultAgainstReferencial )

这是 HTML 输出:

currentlyDisplayedRentability: number
investment: number
updatedAbsoluteResult: number
appropriateReferencialRentability: number
resultAgainstReferencial: number
resultAgainstReferencial: NaN

一切都是数字类型,但是当我想返回最终结果时,我得到“不是数字”的结果。任何人都知道为什么?

享受你的周末!

4

4 回答 4

2

数值类型可以返回 NaN 并且仍然是数值类型。检查 updatedAbsoluteResult、适当的ReferencialRentability 和投资的值以查看其中是否有任何意外值。这个等式中的某些东西很可能出错了。

于 2011-11-11T17:10:58.683 回答
1

问题很可能是 resultAgainstReferencial 是NaN. 你得到的结果是因为:

typeof NaN == "number"

给你看:

resultAgainstReferencial = NaN;
alert(typeof resultAgainstReferencial);   // alerts "number"

你甚至可以在这里看到它:http: //jsfiddle.net/jfriend00/t3ubg/

所以,在上游的某个地方,你试图用不是数字的东西来做数学。您必须查看所有数字输入的值并查看数据出错的地方。

于 2011-11-11T17:13:10.170 回答
0

NaNin javascript 在技术上仍然是 type number。它通常是由除以 0 引起的。对另一个进行任何数学计算NaN也会返回NaN

于 2011-11-11T17:11:24.427 回答
0

也许具有讽刺意味的是,NaN它本身就是一个Number.

您可能在parseInt()某处呼叫失败,或许多其他可能性。没有看到相关代码很难猜到。

于 2011-11-11T17:12:30.543 回答