javascript中数据类型转换

console.log(parseInt(12.90));//将数据转换为整形,结果为12
console.log(parseFloat('31.21ab'));//将数据转换为浮点型,返回值为32.21

注意:parseFloat 只能识别数字,其他的字符串不能识别;若都是字符  输出非数字 (NaN)

  用isNaN()函数检测是否为数字,返回值是布尔值。若是true,则是非数字;反之为数字

console.log(parseFloat('abs'));//结果为NaN 即非数字
document.write(isNaN('asas'));//非数字为true
document.write(isNaN('111'));//数字为false

// 数字转换为字符串两种方式
var a = 123;
b = a.toString();
c = String(a);//方式二
console.log(b,c);
console.log(typeof(b));

原文地址:https://www.cnblogs.com/lvxisha/p/9698518.html