JS operator Conversion number

Source: Internet
Author: User

In node. JS source code, it is common to use various symbols to process strings as numbers. May be written by different people, the use of different styles. Basically there are some of the following.

Convert a string to a number +

A simple way to convert a number string into a number is to prefix it with a number + .

  VarSi= + ";//12 var SN = + ' -12 ' ;// -12 var SF = + ' 12.7 ' ;//12.7 var SS = + ' 12s ' ;//NaN               
*

To convert using multiplication symbols * :

  VarSi= ' 12 ' * 1; 12 Varsn= ' -12 ' * 1;  // -12 var sf = ' 12.7 ' * 1;  //12.7 var ss = ' 12s ' * 1;  //NaN                  
Convert a string to an integer ~ ~

The operator originally intended to be bitwise ~ reversed, using the ability to convert ~~ a string that can be converted to a number into an integer:

  VarSi= ~~' 12 '; 12 Varsn= ~~ ' -12 '   -12 var SF = ~~ ' 12.7 ' ;//12 var SNF = ~~ ' -12.7 ' ;// -12 var SS = ~~ ' 12s ' ;//0                
>>

The operator is intended to be >> a signed right shift, using the ability to convert >> 0 a converted string to an integer:

  VarSi= ' 12 ' >> 0; 12 Varsn= '-12 ' >> 0; -12 Varsf=  ' 12.7 '  >> 0;//12 var SNF =  ' -12.7 '  >>0; // -12 var SS =  ' 12s '  >>0; //0                
>>>

The operator is intended to >>> move right without a symbol, and you can use to >>> 0 convert a string to an integer:

  VarSi= ' 12 ' >>> 0; 12 Varsn=  ' -12 '  >>> Span class= "lit" >0; //4294967284 var SF =< Span class= "PLN" >  12.7 '  >>> Span class= "lit" >0; //12 var SS =  ' 12s '  >>>0; //0                
Attention

When using the above symbol, its conversion is sometimes different from the library that comes with JavaScript. Such as:

  VarSfp= ' 12.4 '; VarSfn= '-12.7 '; VarMsfp= Math.Floor(Sfp //12 var NSFP = ~~sfp; Span class= "com" >//12 var MSFN = math.sfn// -13 var NSFN = ~~sfn;// -12               

There are several scenarios for using symbols instead of JavaScript libraries:

    • The use of symbols may be better than the performance of library functions. However, due to the different JS interpreter, it is possible to reverse the situation.
    • Reduce the transmission of characters, the use of symbols will save a certain amount of space.

At some point, however, you must use library functions. such as: parseInt(‘12s‘) .

JS operator Conversion number

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.