确定 JavaScript 值是否为“整数”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10270648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:35:58  来源:igfitidea点击:

Determine if JavaScript value is an "integer"?

javascriptjquery

提问by Kaizokupuffball

Possible Duplicate:
Check if a variable contains a numerical value in Javascript?

可能的重复:
检查变量是否包含 Javascript 中的数值?

How do I check if variable is an integer in jQuery?

如何检查变量是否是 jQuery 中的整数?

Example:

例子:

if (id == int) { // Do this }

Im using the following to get the ID from the URL.

我使用以下内容从 URL 获取 ID。

var id = $.getURLParam("id");

But I want to check if the variable is an integer.

但我想检查变量是否是整数。

回答by bardiir

Try this:

尝试这个:

if(Math.floor(id) == id && $.isNumeric(id)) 
  alert('yes its an int!');

$.isNumeric(id)checks whether it's numeric or not
Math.floor(id) == idwill then determine if it's really in integer value and not a float. If it's a float parsing it to int will give a different result than the original value. If it's int both will be the same.

$.isNumeric(id)检查它是否是数字,
Math.floor(id) == id然后确定它是否真的是整数值而不是浮点数。如果它是一个浮点数,将其解析为 int 将给出与原始值不同的结果。如果它是 int 两者将是相同的。

回答by Aadit M Shah

Here's a polyfill for the Numberpredicate functions:

这是Number谓词函数的 polyfill :

"use strict";

Number.isNaN = Number.isNaN ||
    n => n !== n; // only NaN

Number.isNumeric = Number.isNumeric ||
    n => n === +n; // all numbers excluding NaN

Number.isFinite = Number.isFinite ||
    n => n === +n               // all numbers excluding NaN
      && n >= Number.MIN_VALUE  // and -Infinity
      && n <= Number.MAX_VALUE; // and +Infinity

Number.isInteger = Number.isInteger ||
    n => n === +n              // all numbers excluding NaN
      && n >= Number.MIN_VALUE // and -Infinity
      && n <= Number.MAX_VALUE // and +Infinity
      && !(n % 1);             // and non-whole numbers

Number.isSafeInteger = Number.isSafeInteger ||
    n => n === +n                     // all numbers excluding NaN
      && n >= Number.MIN_SAFE_INTEGER // and small unsafe numbers
      && n <= Number.MAX_SAFE_INTEGER // and big unsafe numbers
      && !(n % 1);                    // and non-whole numbers

All major browsers support these functions, except isNumeric, which is not in the specification because I made it up. Hence, you can reduce the size of this polyfill:

所有主流浏览器都支持这些功能,除了isNumeric,它不在规范中,因为它是我编造的。因此,您可以减小此 polyfill 的大小:

"use strict";

Number.isNumeric = Number.isNumeric ||
    n => n === +n; // all numbers excluding NaN

Alternatively, just inline the expression n === +nmanually.

或者,只需n === +n手动内联表达式。

回答by Marc

Use jQuery's IsNumeric method.

使用 jQuery 的 IsNumeric 方法。

http://api.jquery.com/jQuery.isNumeric/

http://api.jquery.com/jQuery.isNumeric/

if ($.isNumeric(id)) {
   //it's numeric
}

CORRECTION: that would not ensure an integer. This would:

更正:这不能确保整数。这个会:

if ( (id+"").match(/^\d+$/) ) {
   //it's all digits
}

That, of course, doesn't use jQuery, but I assume jQuery isn't actually mandatory as long as the solution works

那当然不使用 jQuery,但我认为只要解决方案有效,jQuery 实际上并不是强制性的