Blogger Information
Blog 75
fans 0
comment 0
visits 54670
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
小猿圈之Js中String怎么转换为int
聆听的博客
Original
870 people have browsed it

很多做前端的朋友们经常在js中会用到,字符串到整型的一个转换,那么对于小白来说这个转换是很重要的,在面试过程中或者在工作中都会频繁遇到,那么string到js怎么转换呢,小猿圈今天详解一下,大家跟着学习一下吧。

方案一代码:

实例

Number(str)

运行实例 »

点击 "运行实例" 按钮查看在线实例

方案二代码:

实例

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

运行实例 »

点击 "运行实例" 按钮查看在线实例

方案一与方案二对比:


实例

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

运行实例 »

点击 "运行实例" 按钮查看在线实例


注意:parseInt方法在format'00'开头的数字时会当作2进制转10进制的方法进行转换;

所以建议string转int最好用Number方法;

大家领悟到了吧,看起来也很so easy,虽然知识点很小但是是非常有用的,用处很广泛也很重要,不会js的前端感觉不能算真正意义上的前端,所以想要干前端的朋友们一定好好学习js,小猿圈有很多前端的视频,想要了解的,可以去小猿圈学习一下。

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!