Javascript 检查数字是否有小数位/是整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2304052/
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-22 23:41:44  来源:igfitidea点击:

Check if a number has a decimal place/is a whole number

javascriptvalidationnumbersinteger

提问by Hans

I am looking for an easy way in JavaScript to check if a number has a decimal place in it (in order to determine if it is an integer). For instance,

我在 JavaScript 中寻找一种简单的方法来检查一个数字是否有小数位(以确定它是否是整数)。例如,

23 -> OK
5 -> OK
3.5 -> not OK
34.345 -> not OK
if(number is integer) {...}

回答by Andy E

Using moduluswill work:

使用模数将起作用:

num % 1 != 0
// 23 % 1 = 0
// 23.5 % 1 = 0.5


Note that this is based on the numerical valueof the number, regardless of format. It treats numerical strings containing whole numbers with a fixed decimal point the same as integers:

请注意,这是基于数字的数量,不论格式。它将包含带有固定小数点的整数的数字字符串视为与整数相同:

'10.0' % 1; // returns 0
10 % 1; // returns 0
'10.5' % 1; // returns 0.5
10.5 % 1; // returns 0.5

回答by le_m

Number.isInteger(23);  // true
Number.isInteger(1.5); // false
Number.isInteger("x"); // false: 

Number.isInteger()is part of the ES6 standard and not supported in IE11.

Number.isInteger()是 ES6 标准的一部分,在 IE11 中不受支持。

It returns false for NaN, Infinityand non-numeric arguments while x % 1 != 0returns true.

它为和非数字参数返回 false NaNInfinity同时x % 1 != 0返回 true。

回答by Ike

Or you could just use this to find out if it is NOT a decimal:

或者您可以使用它来确定它是否不是小数:

string.indexOf(".") == -1;

回答by Thomas

The most common solution is to strip the integer portion of the number and compare it to zero like so:

最常见的解决方案是去除数字的整数部分并将其与零进行比较,如下所示:

function Test()
{
     var startVal = 123.456
     alert( (startVal - Math.floor(startVal)) != 0 )
}

回答by dYale

Simple, but effective!

简单,但有效!

Math.floor(number) === number;

回答by kennebec

//How about byte-ing it?

//字节处理怎么样?

Number.prototype.isInt= function(){
 return this== this>> 0;
}

I always feel kind of bad for bit operators in javascript-

我总是觉得 javascript 中的位运算符有点不好-

they hardly get any exercise.

他们几乎没有得到任何锻炼。

回答by kennebec

number = 20.5

if (number == Math.floor(number)) {

alert("Integer")

} else {

alert("Decimal")

}

Pretty cool and works for things like XX.0 too! It works because Math.floor() chops off any decimal if it has one so if the floor is different from the original number we know it is a decimal! And no string conversions :)

非常酷,也适用于 XX.0 之类的东西!它起作用是因为 Math.floor() 会去掉任何有小数的小数,所以如果 floor 与原始数字不同,我们就知道它是小数!并且没有字符串转换:)

回答by Christopher Connery

Number.isInteger()is probably the most concise. It returns true if it is an integer, and false if it isn't.

Number.isInteger()可能是最简洁的。如果是整数则返回真,否则返回假。

回答by ghostdog74

var re=/^-?[0-9]+$/;
var num=10;
re.test(num);

回答by Vitor Lins

function isDecimal(n){
    if(n == "")
        return false;

    var strCheck = "0123456789";
    var i;

    for(i in n){
        if(strCheck.indexOf(n[i]) == -1)
            return false;
    }
    return true;
}