确定JavaScript值是否为“整数”? [英] Determine if JavaScript value is an "integer"?

查看:66
本文介绍了确定JavaScript值是否为“整数”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

检查变量是否包含Javascript中的数值?

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

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

示例:

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

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

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

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

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

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

推荐答案

试试这个:

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

$ .isNumeric(id)检查是否它是数字还是不是数字

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

$.isNumeric(id) checks whether it's numeric or not
Math.floor(id) == id will 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.

这篇关于确定JavaScript值是否为“整数”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆