如何在JavaScript中检查变量是否为整数?
本文翻译自:How to check if a variable is an integer in JavaScript?
How do I check if a variable is an integer in JavaScript, and throw an alert if it isn't? 如何检查JavaScript中的变量是否为整数,如果不是,则引发警报? I tried this, but it doesn't work: 我试过了,但是不起作用:
<html><head><script type="text/javascript">var data = 22;alert(NaN(data));</script></head>
</html>
#1楼
参考:https://stackoom.com/question/zPdA/如何在JavaScript中检查变量是否为整数
#2楼
Check if the variable is equal to that same variable rounded to an integer, like this: 检查变量是否等于舍入为整数的相同变量,如下所示:
if(Math.round(data) != data) {alert("Variable is not an integer!");
}
#3楼
First off, NaN is a "number" (yes I know it's weird, just roll with it), and not a "function". 首先,NaN是一个“数字”(是的,我知道这很奇怪,随它滚动),而不是一个“函数”。
You need to check both if the type of the variable is a number, and to check for integer I would use modulus. 您需要检查变量的类型是否为数字,并且要检查整数我将使用模数。
alert(typeof data === 'number' && data%1 == 0);
#4楼
You could check if the number has a remainder: 您可以检查数字是否有余数:
var data = 22;if(data % 1 === 0){// yes it's an integer.
}
Mind you, if your input could also be text and you want to check first it is not, then you can check the type first: 请注意,如果您输入的内容也可以是文本,并且您要首先检查它是否不是文本,那么可以先检查类型:
var data = 22;if(typeof data === 'number'){// yes it is numericif(data % 1 === 0){// yes it's an integer.}
}
#5楼
Use the === operator ( strict equality ) as below, 使用===运算符( 严格等于 ),如下所示,
if (data === parseInt(data, 10))alert("data is integer")
elsealert("data is not an integer")
#6楼
Assuming you don't know anything about the variable in question, you should take this approach: 假设您对所讨论的变量一无所知,则应采用以下方法:
if(typeof data === 'number') {var remainder = (data % 1);if(remainder === 0) {// yes, it is an integer}else if(isNaN(remainder)) {// no, data is either: NaN, Infinity, or -Infinity}else {// no, it is a float (still a number though)}
}
else {// no way, it is not even a number
}
To put it simply: 简而言之:
if(typeof data==='number' && (data%1)===0) {// data is an integer
}
如何在JavaScript中检查变量是否为整数?相关推荐
- 如何在 JavaScript 中检查数字是正数还是负数
如何在 JavaScript 中检查数字是正数还是负数 前几天一个月薪35k的兄弟,给我推了一个人工智能学习网站,看了一段时间挺有意思的.包括语音识别.机器翻译等从基础到实战都有,很详细,分享给大家. ...
- regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾
regexp 好汉字符串 by Catherine Vassant (aka Codingk8) 由凯瑟琳·瓦森(Catherine Vassant)(又名Codingk8) 如何在JavaScrip ...
- !! javascript_产量! 产量! 生成器如何在JavaScript中工作。
!! javascript by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 产量! 产量! 生成器如何在JavaScript中工作. (Yield! Yiel ...
- html类型转换函数,如何在JavaScript中转换数据类型?
在JavaScript中,数据类型用于对一种特定类型的数据进行分类,确定可以分配给类型的值以及可以对其执行的操作.虽然由于类型强制,JavaScript会自动转换许多值,但为了达到预期的结果,通常最好 ...
- 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法
by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法 (How to use ...
- 如何在 JavaScript 中实现拖放
来源:http://www.javaeye.com/post/152142 译者说明: 终于完成了全文的翻译,由于时间比较参促,文章没有过多的校正与润色,阅读过程中难免会有些许生硬或不准确的感觉,请大 ...
- 如何在JavaScript中声明名称空间?
如何在JavaScript中创建名称空间,以使我的对象和函数不会被其他同名对象和函数覆盖? 我使用了以下内容: if (Foo == null || typeof(Foo) != "obje ...
- 如何在 JavaScript 中获取当前日期?
问: 想要改进这篇文章?提供这个问题的详细答案,包括引文和解释为什么你的答案是正确的.没有足够细节的答案可能会被编辑或删除. 如何在 JavaScript 中获取当前日期? 答1: HuntsBot周 ...
- java+script+当前日期_如何在JavaScript中获取当前日期?
如何在JavaScript中获取当前日期? #1楼 您可以使用扩展了 Date对象的Date.js库,从而可以使用.today()方法. #2楼 如果您想对日期格式进行更多的粒度控制,我强烈建议您查看 ...
最新文章
- 三方博弈matlab_小星星的读研日记之电商动态博弈知多少?
- jquery 使用animate来改变高度自动添加样式overflow:hidden的问题
- 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
- Vb.net/VB 声明API功能父窗口功能
- 【华为云技术分享】如何设计高质量软件-领域驱动设计DDD(Domain-Driven Design)学习心得
- 几点减几点怎么列算式_洁净室平面设计怎么做?洁净室施工做到这几点就对了...
- python读取压缩文件的指定后缀的文件_python打包压缩、读取指定目录下的指定类型文件...
- MySQL查询语句格式总结
- 简述23种软件设计模式
- 离线发布bolg----使用Windows live writer客户端来发布CSDN的博客文章
- 极客大学产品经理训练营 产品思维和产品意识(上) 第3课总结
- 操作系统原理基础知识
- 亲戚B (普通并查集)
- margin外边距合并问题以及解决方式
- 大端与小端字节数据详解
- 第八届蓝桥杯JavaB组省赛真题
- Java--集合--经典七道综合练习题
- 基于单片机太阳能自动双轴追光电路仿真系统(毕设课设)
- excel控件只为简单写入数据表--github找到ExcelUtil笔记
- 愿世界永无 bug!再也不想熬夜加班了!
热门文章
- PHP安全编程:register_globals的安全性
- 休闲娱乐游戏-贱鸟跳跳
- 第一章,安装 composer
- IBM AIX创建lv
- 模板与泛型编程(二)
- Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法System.String ToString()问题解决)...
- 利用React/anu编写一个弹出层
- 2017年4月14日
- WPF RichTextBox 控件常用方法和属性
- python-appium手机自动化测试(仅需安装包)前期准备(pydev-eclipse编辑器)