Number
数字+字符串
const str1 = '10'
const num1 = 20
console.log(str1 + num1); // 1020
数字运算符+会将字符串转换为数字
const str1 = '10'
const str2 = '20'
console.log(+str1 + +str2); // 30
console.log(str1 / str2); // 0.5
字符串数字进行比较
const str1 = '10'
const str2 = '20'
console.log(str1 < str2); // true
isNaN()
判断是否为数字,字符串数字也为false
const str1 = 'aaa'
const str2 = '10'
const num1 = 20
console.log(isNaN(str1)); // true
console.log(isNaN(str2)); // false
console.log(isNaN(num1)); // false
typeof NaN // number
typeof Infinity // 超出数值范围 number
输出进制数据
const num1 = 20
console.log(num1.toString(2)); // 10100
console.log(num1.toString(8)); // 24
console.log(num1.toString(16)); // 14
const num1 = new Number(10)
const num2 = new Number(10)
console.log(num1 == num2); // false
数字方法
toString() | 将任何类型数字(变量,表达式等)转换为字符串 |
toExponential(length) | 将数字规定小数点长度形式返回 |
toFiexed(length) | 返回指定小数长度的字符串 |
toPrecision(length) | 返回带整数指定长度的字符串 |
const num1 = 10.879
console.log(num1.toFixed(2)); // 10.88
console.log(num1.toPrecision(2)); // 11