var num =10.005;//按照指定的小数位返回数值的字符串
console.log(num.toFixed(1));//10.0
console.log(num.toFixed(2));//10.01var num =100;//指数表示法
console.log(num.toExponential(1));//1.0e+2
console.log(num.toExponential(2));//1.00e+2var num =99;//可能返回固定大小格式,也可能返回指数格式
console.log(num.toPrecision(1));//1e+2
console.log(num.toPrecision(2));//99
3、String
(1) 字符串的对象包装类型
var stringObj =newString('hello world')
console.log(stringObj.length);
(2)字符方法:charAt、charCodeAt
var stringValue ='hello world'
console.log(stringValue.charAt(6));//w 返回指定位置的字符
console.log(stringValue.charCodeAt(6));//119 返回指定位置的字符编码
console.log(stringValue[0]);//ie7后支持
(3)字符串操作方法:concat、slice、substring、substr
var stringValue ='hello world'var stringValue2 ='!'
console.log(stringValue.concat(stringValue2,'你好,世界!'));//hello world! 对字符串的拼接
console.log(stringValue.slice(3,7));//lo w 切片 [开始,结束位置)
console.log(stringValue.substring(3,7));//lo w 切片 [开始,结束位置)
console.log(stringValue.substr(3,7));//lo worl 切片 [开始,字符个数)
console.log(stringValue.slice(-3));//rld slice将-3 (+11)转换为slice(8)
console.log(stringValue.substr(-3));//rld substr-3 (+11)substr(8)
console.log(stringValue.substring(-3));//hello world substring将-3转换为0,返回全部字符串
(4)字符串位置方法:indexOf、lastIndexOf
var stringValue ='hello world'
stringValue.indexOf('o')//4
stringValue.lastIndexOf('o')//7
(5)trim( ) :删除前置和后置的空格并返回结果
var stringValue =' hello,world '
stringValue.trim()//hello,world
var stringValue ='hello world'
stringValue.toLocaleUpperCase()//HELLO WORLD
stringValue.toUpperCase())//HELLO WORLD
stringValue.toLocaleLowerCase(0)//hello world
stringValue.toLowerCase()//hello world