Javascript 如何检查数字是浮点数还是整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3885817/
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
How do I check that a number is float or integer?
提问by coure2011
How to find that a number is float
or integer
?
如何找到一个数字是float
或integer
?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
回答by kennebec
check for a remainder when dividing by 1:
除以 1 时检查余数:
function isInt(n) {
return n % 1 === 0;
}
If you don't know that the argument is a number you need two tests:
如果你不知道参数是一个数字,你需要两个测试:
function isInt(n){
return Number(n) === n && n % 1 === 0;
}
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
Update 20195 years after this answer was written, a solution was standardized in ECMA Script 2015. That solution is covered in this answer.
在编写此答案 5 年后更新 2019,在 ECMA Script 2015 中标准化了一个解决方案。此答案涵盖了该解决方案。
回答by Dagg Nabbit
Try these functions to test whether a value is a number primitive value that has no fractional part and is within the size limits of what can be represented as an exact integer.
试试这些函数来测试一个值是否是一个没有小数部分的数字原始值,并且在可以表示为一个精确整数的大小限制内。
function isFloat(n) {
return n === +n && n !== (n|0);
}
function isInteger(n) {
return n === +n && n === (n|0);
}
回答by warfares
Why not something like this:
为什么不是这样的:
var isInt = function(n) { return parseInt(n) === n };
回答by paperstreet7
There is a method called Number.isInteger()
which is currently implemented in everything but IE. MDNalso provides a polyfill for other browsers:
有一个被调用的方法Number.isInteger()
,它目前在除 IE 之外的所有东西中都实现了。MDN还为其他浏览器提供了 polyfill:
Number.isInteger = Number.isInteger || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};
However, for most uses cases, you are better off using Number.isSafeInteger
which also checks if the value is so high/low that any decimal places would have been lost anyway. MDNhas a polyfil for this as well. (You also need the isInteger
pollyfill above.)
但是,对于大多数用例,最好使用Number.isSafeInteger
which 还检查值是否太高/太低以至于任何小数位都会丢失。MDN也为此提供了一个 polyfil。(您还需要isInteger
上面的pollyfill。)
if (!Number.MAX_SAFE_INTEGER) {
Number.MAX_SAFE_INTEGER = 9007199254740991; // Math.pow(2, 53) - 1;
}
Number.isSafeInteger = Number.isSafeInteger || function (value) {
return Number.isInteger(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER;
};
回答by Marcio Mazzucato
You can use a simple regular expression:
您可以使用一个简单的正则表达式:
function isInt(value) {
var er = /^-?[0-9]+$/;
return er.test(value);
}
Or you can use the below functions too, according your needs. They are developed by the PHPJS Project.
或者您也可以根据需要使用以下功能。它们由PHPJS 项目开发。
is_int()
=> Check if variable type is integer and if its content is integer
is_int()
=> 检查变量类型是否为整数以及其内容是否为整数
is_float()
=> Check if variable type is float and if its content is float
is_float()
=> 检查变量类型是否为浮点型以及其内容是否为浮点型
ctype_digit()
=> Check if variable type is string and if its content has only decimal digits
ctype_digit()
=> 检查变量类型是否为字符串以及其内容是否只有十进制数字
Update 1
更新 1
Now it checks negative numbers too, thanks for @ChrisBartley comment!
现在它也检查负数,感谢@ChrisBartley 评论!
回答by Tal Liron
Here are efficient functions that check if the value is a number or can be safely converted toa number:
以下是检查值是否为数字或可以安全地转换为数字的高效函数:
function isNumber(value) {
if ((undefined === value) || (null === value)) {
return false;
}
if (typeof value == 'number') {
return true;
}
return !isNaN(value - 0);
}
And for integers (would return false if the value is a float):
对于整数(如果值为浮点数,则返回 false):
function isInteger(value) {
if ((undefined === value) || (null === value)) {
return false;
}
return value % 1 == 0;
}
The efficiency here is that parseInt (or parseNumber) are avoided when the value already is a number. Both parsing functions alwaysconvert to string first and then attempt to parse that string, which would be a waste if the value already is a number.
这里的效率是当值已经是数字时避免使用 parseInt(或 parseNumber)。两个解析函数总是先转换为字符串,然后尝试解析该字符串,如果值已经是数字,这将是一种浪费。
Thank you to the other posts here for providing further ideas for optimization!
感谢这里的其他帖子为优化提供了进一步的想法!
回答by shime
function isInteger(x) { return typeof x === "number" && isFinite(x) && Math.floor(x) === x; }
function isFloat(x) { return !!(x % 1); }
// give it a spin
isInteger(1.0); // true
isFloat(1.0); // false
isFloat(1.2); // true
isInteger(1.2); // false
isFloat(1); // false
isInteger(1); // true
isFloat(2e+2); // false
isInteger(2e+2); // true
isFloat('1'); // false
isInteger('1'); // false
isFloat(NaN); // false
isInteger(NaN); // false
isFloat(null); // false
isInteger(null); // false
isFloat(undefined); // false
isInteger(undefined); // false
回答by Deepak Yadav
function isInt(n)
{
return n != "" && !isNaN(n) && Math.round(n) == n;
}
function isFloat(n){
return n != "" && !isNaN(n) && Math.round(n) != n;
}
works for all cases.
适用于所有情况。
回答by Claudiu
As others mentioned, you only have doubles in JS. So how do you define a number being an integer? Just check if the rounded number is equal to itself:
正如其他人提到的,您在 JS 中只有双打。那么如何定义一个数是整数呢?只需检查四舍五入的数字是否等于自身:
function isInteger(f) {
return typeof(f)==="number" && Math.round(f) == f;
}
function isFloat(f) { return typeof(f)==="number" && !isInteger(f); }
回答by Goehybrid
How about this one?
这个怎么样?
isFloat(num) {
return typeof num === "number" && !Number.isInteger(num);
}