JavaScript - 添加2个整数根本不起作用

时间:2017-11-10 18:18:17

标签: javascript math random integer nan

当我尝试将两个整数加在一起时,整数保持原来的状态。这很难解释,所以这是我的代码:

var levelRewardsID = parseInt(resultData[0].levelRewardsHighestID)
var levelRewardsIDIncrease = Math.floor(Math.random()*9)+1
var newLevelRewardsID = levelRewardsID+levelRewardsIDIncrease
console.log(levelRewardsID)
console.log(levelRewardsIDIncrease)
console.log(newLevelRewardsID)
if (!isNaN(levelRewardsID)) {
  console.log("not NAN")
}
if (!isNaN(levelRewardsIDIncrease)) {
  console.log("not NAN 2")
}
if (!isNaN(newLevelRewardsID)) {
   console.log("not NAN 3")
}

控制台:

89819672607051330000
6
89819672607051330000
not NAN
not NAN 2
not NAN 3

所以你可以看到,一切都是整数。所以这不是问题。然而,当我尝试var newLevelRewardsID = levelRewardsID+levelRewardsIDIncrease时,输出与levelRewardsID相同......我不确定我做错了什么,但如果有人知道,请告诉我。谢谢!

注意:我正在使用Node.JS版本9.1.0(发布时最新)如果有帮助

1 个答案:

答案 0 :(得分:2)

我认为你达到javascript数量限制,似乎是9007199254740992和89819672607051330000> 9007199254740992。

请参阅What is JavaScript's highest integer value that a number can go to without losing precision?