Js small circle of ape in String how to int

Many do the front of friends is often used in js in a string to integer conversion, then white is for this conversion is very important, in the interview process or are frequently encountered in the work, then string js how to convert it, a small circle ape today Detailed look, we follow to learn about it.

Option One Code:

Number(str)复制代码

Option II Code:

//parseInt 方法都有两个参数, 第一个参数就是要转换的对象, 第二个参数是进制基数, 可以是 2, 8, 10, 16, 默认以 10 进制处理
parseInt(str)
复制代码

A comparison program to program two:

var str='1250' ;
alert( Number(str) );  //得到1250
alert(parseInt(str));  //得到1250
var str1='00100';
alert( Number(str1) );  //得到100
alert(parseInt(str1));  //得到64
复制代码

Note: parseInt method when format'00 number at the beginning of 'transfer method decimal binary conversion will be treated;

It is recommended by the best string transfected int Number Method;

We realized it, looks very so easy, though knowledge is small but very useful, very broad use is also very important, not js front-end feeling on the front end can not be considered a real sense, so I want to dry the front friends must learn js, small ape circle have a lot of front-end video, want to know, you can go to a small circle ape to learn about.


Reproduced in: https: //juejin.im/post/5d088b606fb9a07ed911c3c8

Guess you like

Origin blog.csdn.net/weixin_33795743/article/details/93181639