Introduction to N methods for determining integer types in JavaScript _ javascript skills

Source: Internet
Author: User
Tags integer numbers
This article mainly introduces N methods for determining Integer type in JavaScript. For more information, see Integer type in JavaScript. In ECMAScript specifications, they only exist in concepts:

All digits are floating-point numbers, and integers are not a group of digits without decimal places.

In this blog, I will explain how to check whether a value is an integer.
ECMAScript 5

There are many methods you can use in ES5. Sometimes, you may want to use your own method: An isInteger (x) function. If it is an integer, return true; otherwise, return false.

Let's take a look at some examples.

Pass the remainder check

You can use the remainder operation (%) to calculate the remainder of a number by 1 and see if the remainder is 0.

function isInteger(x) {  return x % 1 === 0;}

I like this method because it is very simple and effective.

> isInteger(17)true> isInteger(17.13)false

Be careful when operating the remainder operation, because the result depends on the symbol of the first number. If it is positive, the result is positive; otherwise, the result is negative.

> 3.5 % 10.5> -3.5 % 1-0.5

Then, we can also check 0, which is not a problem. But the problem is: This method will return true for non-numbers, because % will convert it to a number:

> isInteger('')true> isInteger('33')true> isInteger(false)true> isInteger(true)true

You can solve this problem through a simple type check:

function isInteger(x) {  return (typeof x === 'number') && (x % 1 === 0);}

Math. round () is an integer if a number is rounded up and is the same as the previous value. In JavaScript, you can use Math. round () to check:

function isInteger(x) {  return Math.round(x) === x;}

This method is also good

> isInteger(17)true> isInteger(17.13)false

It can also be used to determine non-numbers, because Math. round () always returns a number and = returns true only when the type is the same.

> isInteger('')false

If you want to make the code clearer, you can add the type check (which we did in previous versions ). In addition, Math. floor () and Math. ceil () can work like Math. round. The bitwise operation checks the bitwise operator to provide another method of "Rounding:

function isInteger(x) {  return (x | 0) === x;}

This solution (like other bitwise operations) has a defect that it cannot process numbers that exceed 32 bits.

> isInteger(Math.pow(2, 32))false

Using parseInt () to check parseInt () also provides a method similar to Math. round () to convert a number into an integer. Let's see why this method is good.

function isInteger(x) {  return parseInt(x, 10) === x;}

Like the Math. round () solution, it can also handle non-numeric situations, but it cannot process all integer numbers correctly:

> isInteger(1000000000000000000000)false

Why? ParseInt () forces the first parameter to be parsed into a string before the integer is parsed. Therefore, using this method to convert a number to an integer is not a good choice.

> parseInt(1000000000000000000000, 10)1> String(1000000000000000000000)'1e+21'

As above, parseInt () stops processing at 1 when parsing '1e + 21', so it returns 1. ECMAScript 6 for Math. in addition, ES6 provides another method to convert a number into an integer: Math. trunc (). This function removes the decimal part of a number.

> Math.trunc(4.1)4> Math.trunc(4.9)4> Math.trunc(-4.1)-4> Math.trunc(-4.9)-4

In addition, ECMAScript6 does not need to process trivial tasks that check integers because it has a built-in function Number. isInteger ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.