Home  >  Article  >  Web Front-end  >  What are the methods for converting javascript to integers?

What are the methods for converting javascript to integers?

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-21 15:06:043689browse

The methods for converting JavaScript into integers are: 1. Use the parseInt() method, the syntax format is "parseInt (string, the base of the number to be parsed)"; 2. Use the Math.trunc() method, the syntax format is "Math.trunc(value)".

What are the methods for converting javascript to integers?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

This article will enumerate and explain JavaScript related methods to convert a number (or numerical object) into an integer.

Use parseInt

The syntax of parseInt is as follows: parseInt(string, radix)

The parameter string represents the string to be parsed, or it can be an object, which will be called automatically The object's toString function gets the string to be parsed.

The second parameter of parseInt can specify the base of the number to be parsed. Note that the value is between 2 ~ 36. If the parameter is less than 2 or greater than 36, parseInt() will return NaN. For example, in the following code, the result is 8, which can easily convert other base numbers into decimal numbers:

parseInt(10,8) // 结果为8

When the value of the parameter radix is ​​0, or the parameter is not set , parseInt() will determine the base of the number based on the string.

For example, if string starts with "0x", parseInt() will parse the rest of string into hexadecimal integers. If a string begins with 0, ECMAScript v3 allows an implementation of parseInt() to parse the following characters as octal or hexadecimal digits. If string starts with a number from 1 to 9, parseInt() will parse it into a decimal integer.

Notes

1. Only the first number in the string will be returned. What does it mean? If the input string is "123abc", "123,123", then the result is 123, and the parseInt method will automatically ignore the following non-numeric parts.

2. Spaces at the beginning and end of the input string are allowed.

3. parseFloat also has the above two characteristics, but this article will not focus on them.

Use Math.trunc

The Math.trunc() method will remove the decimal part of the number, leaving only the integer part. For example, the following code:

Math.trunc(13.37)    // 13
Math.trunc(42.84)    // 42
Math.trunc(0.123)    //  0
Math.trunc(-0.123)   // -0
Math.trunc("-1.123") // -1
Math.trunc(NaN)      // NaN
Math.trunc("foo")    // NaN
Math.trunc()         // NaN

When the incoming type is not a number, addictive conversion will be performed automatically. But if it is a non-numerical parameter, NaN is returned.

IE browser does not support this method, so you can consider polyfill:

Math.trunc || (Math.trunc = function(v){
   return v < 0 ? Math.ceil(v) : Math.floor(v); // 使用Math.floor和Math.ceil方法
})
// 或者
if (!Math.trunc) {
    Math.trunc = function(v) {
        v = +v;
        if (!isFinite(v)) return v;
        
        return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
        
        // 返回:
        //  0        ->  0
        // -0        -> -0
        //  0.2      ->  0
        // -0.2      -> -0
        //  0.7      ->  0
        // -0.7      -> -0
        //  Infinity ->  Infinity
        // -Infinity -> -Infinity
        //  NaN      ->  NaN
        //  null     ->  0
    };
}

Binary bit operation

For numerical object n, you can convert it to Integer:

  • ~~n Double bitwise NOT

  • n | n bitwise OR operation (Bitwise OR)

  • n | Bitwise OR with 0 (Bitwise OR with 0)

  • n << 0 bit left shift operation 0 bit ( Bitwise left shift)

  • n >> 0 bit right shift operation 0 bit (Bitwise right shift)

  • n & n is AND Operation (Bitwise AND)

For example, the code is as follows:

   ~~1.23 // 1
   -1.2 | -1.2  // - 1
   -1.2 | 0  // - 1
   3.4 >> 0 // 3
   3.2 << 0 // 3
   5.5 & 5.5 // 5
  ~~"1.2" // 1.2

Note, as can be seen from the above, strings will automatically convert numbers under bitwise operations.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What are the methods for converting javascript to integers?. 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