Home  >  Article  >  Web Front-end  >  How to determine whether jquery is an integer

How to determine whether jquery is an integer

藏色散人
藏色散人Original
2020-12-11 10:27:312829browse

How jquery determines whether it is an integer: 1. Use the remainder operator to determine; 2. Use "Math.round" to determine; 3. Judge through parseInt; 4. Judge through bit operations; 5. Through ES6 The provided Number.isInteger determines whether it is an integer.

How to determine whether jquery is an integer

#The operating environment of this tutorial: Windows 7 system, jquery1.10.0 version, thinkpad t480 computer.

Recommended: jquery video tutorial

js determines whether it is an integer type (5 ways)

Method 1. Use The remainder operator determines that any integer is divisible by 1, that is, the remainder is 0. Use this rule to determine whether it is an integer.

function isInteger(obj) {
 return obj%1 === 0
}
isInteger(3) // true
isInteger(3.3) // false 
isInteger('') // true
isInteger('3') // true
isInteger(true) // true
isInteger([]) // true

Returns true for empty strings, string type numbers, Boolean true, and empty arrays. If you are interested in the internal conversion details of these types, please refer to: Weird False Values ​​in JavaScript

Therefore, you need to first determine whether the object is a number, such as adding a typeof

function isInteger(obj) {
 return typeof obj === 'number' && obj%1 === 0
}
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

Method 2, Use Math.round, Math.ceil, and Math.floor to determine whether the

integer is still equal to itself after rounding. Use this feature to determine whether it is an integer, Math.floor example, as follows

function isInteger(obj) {
 return Math.floor(obj) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

Method 3. Determine through parseInt

function isInteger(obj) {
 return parseInt(obj, 10) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false、
//很不错,但也有一个缺点
isInteger(1000000000000000000000) // false

The reason is that parseInt forces the first parameter to be parsed before parsing the integer. String. This method of converting numbers to integers is not a good choice.

Method 4. Determine through bit operations

function isInteger(obj) {
 return (obj | 0) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false
//这个函数很不错,效率还很高。但有个缺陷,上文提到过,位运算只能处理32位以内的数字,对于超过32位的无能为力
isInteger(Math.pow(2, 32)) // 32位以上的数字返回false了

Method 5. ES6 provides Number.isInteger

Number.isInteger(3) // true
Number.isInteger(3.1) // false
Number.isInteger('') // false
Number.isInteger('3') // false
Number.isInteger(true) // false
Number.isInteger([]) // false

Currently, the latest Firefox and Chrome already support it.

The above is the detailed content of How to determine whether jquery is an integer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn