js中出现NaN解决方案

问题:字符串与数字类型转换过程中出现问题

解决:使用Number(),进行强制类型转换,

也可以用:parseFloat()  parseInt()

我自己一个去小数点的代码:

 

function panduanAREA(mingcheng, canhsu, danwei) {
        var dayin;
        if (canhsu !== " " && canhsu !== "" && canhsu !== "0" && canhsu !== null && canhsu !== "Null"&& canhsu !== 0) {
            var canhsu_str = canhsu.toString();
            var or = canhsu_str.indexOf('.');
            if(or >= 0){
                var canhsu_num = Number(canhsu_str.substring(0, canhsu_str.indexOf('.')));
                dayin = "" + mingcheng + ":" + "" + canhsu_num + danwei + "
"; } else { dayin = "" + mingcheng + ":" + "" + canhsu_str + danwei + "
"; } } else { dayin = ""; } return dayin; }

 

你可能感兴趣的:(js中出现NaN解决方案)