Jquery - 奇怪的数字到字符串的转换
我遇到了奇怪的不需要的 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
一切都是 Number 类型,但是当我想返回最终结果时,我得到 'is not a数”作为结果。 有人知道为什么吗?
祝你周末愉快!
I am encountering a strange unwanted Number
to NaN
conversion, without knowing where and why. I am clearly missing something here but I cannot figure out what.
Any suggestion is most welcome ! ;)
Here is the javascript code:
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 )
Here is the HTML output:
currentlyDisplayedRentability: number
investment: number
updatedAbsoluteResult: number
appropriateReferencialRentability: number
resultAgainstReferencial: number
resultAgainstReferencial: NaN
Everything is of Number type, but when I want to return the final result, I get 'is not a number' as a result.
Anyone knows why ?
Enjoy your weekends !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
数字类型可以返回 NaN 并且仍然是数字类型。检查updatedAbsoluteResult、propertyReferencialRentability 和investment 的值,看看其中是否有任何意外值。很可能这个等式中的某些东西出了问题。
A numeric type can return NaN and still be of numeric type. Check the values of updatedAbsoluteResult, appropriateReferencialRentability, and investment to see if any of those are unexpected values. Likely something in that equation has gone awry.
问题可能是 resultAgainstReferencial 为
NaN
。您得到的结果是因为:向您展示:
您甚至可以在这里看到它:http://jsfiddle。 net/jfriend00/t3ubg/
因此,在上游的某个地方,您正在尝试使用非数字的东西进行数学运算。您必须查看所有数字输入的值并查看数据哪里出了问题。
The problem is likely that resultAgainstReferencial is
NaN
. You are getting the results you are because:To show you:
You can even see it here: http://jsfiddle.net/jfriend00/t3ubg/
So, somewhere upstream, you're trying to do math with things that aren't numbers. You'll have to look at the values of all the numeric inputs and see where the data has gone awry.
JavaScript 中的
NaN
从技术上来说仍然是number
类型。它通常是由除以 0 引起的。对另一个NaN
进行任何数学计算也将返回NaN
。NaN
in javascript is technically still of typenumber
. It is usually caused from a division by 0. Doing any mathmatical calculations on anotherNaN
will also returnNaN
.也许具有讽刺意味的是,
NaN
本身就是一个Number
。您可能在某处调用了失败的
parseInt()
,或者还有许多其他可能性。如果没有看到相关代码,很难猜测。Perhaps ironically,
NaN
is itself aNumber
.You may have a failed
parseInt()
call somewhere, or numerous other possibilities. It's hard to guess without seeing the relevant code.