ES6 常用API详解

转载请注明预见才能遇见的博客:http://my.csdn.net/

原文地址:http://blog.csdn.net/pcaxb/article/details/56017311

ES6 常用API详解

字符串转Int和Float类型,JS没有单双精度之分,只有Float没有Double,所以也没有parseDouble。单精度在内存中占32位4个字节有效数字6-7位,双精度占64位8个字节有效数字15-16位,一般常量都是双精度处理的,float a = 0.5报错,float a = 0.5f正确,double a = 0.5正确。

1. 字符串转int float等,没有parseDouble,JS没有单双精度之分,只有Float没有Double

        //字符串转int float等,没有parseDouble
        //JS没有单双精度之分,只有Float没有Double
        let number = 1.2354;
        let numberStr = number.toString();
        console.log(parseFloat(numberStr));//1.2354
        console.log(parseInt(numberStr));//1
2.isNaN判断是不是数字,true是非数字,false是数字

        //isNaN判断是不是数字,true是非数字,false是数字
        console.log(isNaN(number));//false 是数字
3.toFixed()方法可把Number四舍五入为指定小数位数的数字,最后一位四舍五入,位数不够就补0,返回的是字符串
        //toFixed()方法可把Number四舍五入为指定小数位数的数字
        //最后一位四舍五入,位数不够就补0,返回的是字符串
        console.log(number.toFixed(2));//1.24
4.JSON对象和字符串相互转换
        //JSON对象和字符串相互转换
        let personStr = '{"name":"pca"}';//Json字符串
        //Json字符串转Json对象
        let personObj = JSON.parse(personStr);
        console.log(personObj);
        //Json对象转Json字符串
        console.log(JSON.stringify(personObj));

ES6常用API详解

博客地址:http://blog.csdn.net/pcaxb/article/details/56017311



你可能感兴趣的:(#,JavaScriptES6系列)