Jquery - 奇怪的数字到字符串的转换

发布于 2024-12-15 07:27:58 字数 1155 浏览 2 评论 0原文

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

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

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陪你到最终 2024-12-22 07:27:58

数字类型可以返回 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.

陌路黄昏 2024-12-22 07:27:58

问题可能是 resultAgainstReferencial 为 NaN。您得到的结果是因为:

typeof NaN == "number"

向您展示:

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

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

因此,在上游的某个地方,您正在尝试使用非数字的东西进行数学运算。您必须查看所有数字输入的值并查看数据哪里出了问题。

The problem is likely that resultAgainstReferencial is NaN. You are getting the results you are because:

typeof NaN == "number"

To show you:

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

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.

抽个烟儿 2024-12-22 07:27:58

JavaScript 中的 NaN 从技术上来说仍然是 number 类型。它通常是由除以 0 引起的。对另一个 NaN 进行任何数学计算也将返回 NaN

NaN in javascript is technically still of type number. It is usually caused from a division by 0. Doing any mathmatical calculations on another NaN will also return NaN.

你在我安 2024-12-22 07:27:58

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

您可能在某处调用了失败的 parseInt() ,或者还有许多其他可能性。如果没有看到相关代码,很难猜测。

Perhaps ironically, NaN is itself a Number.

You may have a failed parseInt() call somewhere, or numerous other possibilities. It's hard to guess without seeing the relevant code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文