将字符串转换成数字

  1. parseInt()
    字符串第一个字符是数字,直到碰到小数点或者其他字符
var a = parseInt('10.36aa')//10
var a=parseInt('.10.36aa')//NaN
  1. parseFloat()
    如果第一个字符是小数点,则以0.开头直到遇到第二个小数点或者其他字符
var a = parseFloat('.36.33aa')//0.36
var a = parseFloat('10.36.33aa')//10.36
var a = parseFloat('a.36.33aa')//NaN
  1. +
var a = +'a.36.33aa'//NaN
var a = +'.36.33aa'//NaN
var a = +'0.36.33'//NaN
var a = +'0.36'//0.36
  1. Number()
    强制转换
var a = Number('.36')//0.36
var a = +'a.36.33aa'//NaN
var a = +'.36.33aa'//NaN
var a = +'0.36.33'//NaN

你可能感兴趣的:(将字符串转换成数字)