Javascript 中带 (.0) 位小数的数字被认为是整数?

Numbers with (.0) decimals are considered integers in Javascript?

我正在验证它应该只允许整数,我有这样的东西:

let value = '32230.13';

if(!!Number.isInteger(+value.replace(/[^0-9\.]/g, ''))) {
  // stuff
}

绝对不是最漂亮的方法,但在我们得到以 .0 结尾的值之前,它对我们来说工作得很好。所以我注意到以 .0 结尾的值被认为是整数。例如:

console.log(Number.isInteger(100.0)) // expected `false` got `true`
console.log(Number.isInteger(100.00)) // expected `false` got `true`

所以我开始对整数进行一些研究,我找到了这个答案 , but it made me think that it was some of those weird JavaScript issues that we have to deal with when working with Javascript (WTFJS). I even found something similar https://github.com/denysdovhan/wtfjs 甚至用不同的编程语言尝试了同样的事情,我得到了 100.0 的预期结果不是整数,但我还有一些问题:

Javascript标准实际上非常清楚“数学值”和Numbers之间的区别,用语言表示(https://262.ecma-international.org/11.0/#sec-mathematical-operations)

从数学的角度来看,100.0绝对是一个整数。从 JS 的角度来看,isInteger(100.0)true,因为 isInteger 被定义为 return true for Numbers 其数学值为整数。

您的不确定性来自其他一些语言的事实 100100.0 确实是完全不同的东西,具有不同的存储机制和对它们执行的操作的性质。在 Javascript 中,我们实际上不知道特定数字是如何存储的,也无法控制。该标准表示所有 Numbers 的行为都应该像它们都是 IEEE 754-2019 双精度值一样,但是当您计算 1+2 时特定引擎实际执行的操作顺序是什么以及它们与 1.2+3.4 取决于该引擎。