Number,parseInt,parseFloat转换
fwxstar 2018-06-04 580次浏览
摘要:
//可以转换
var a1='';
alert(Number(a1)); //0
var a2=true;
alert(Number(a2)); ...
//可以转换 var a1=''; alert(Number(a1)); //0 var a2=true; alert(Number(a2)); //true=>1 false=>0 var a3=[1]; var a4=['']; alert(Number(a3)); //1 alert(Number(a4)); //0 var a5=null; alert(Number(a5)); //0 //无法转换 var json={}; alert(Number(json)); //NaN var u; alert(Number(u)); //NaN var af= function () { alert(1) }; alert(Number(af)); //NaN
/*parseInt*/ var t="200px"; console.log(parseInt(t)); //200 console.log(Number(t)); //NaN /*parseFloat*/ var t2=200.355; console.log(parseInt(t2)); //200 console.log(parseFloat(t2)); //200.355 console.log(Number(t2)); //200.355 //判断是是否整数 if(parseInt(t2) === parseFloat(t2)){ // t2是整数 }else{ // 非整数 }
- 上一篇:显示类型 隐式类型转换 及NaN详解
- 下一篇:this详解