文章来源地址https://www.uudwc.com/A/qRyve/文章来源:https://www.uudwc.com/A/qRyve/
数值扩展
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数值扩展</title>
</head>
<body>
<script>
// 0.Number.EPSION 是javaScript 表示的最小精度
//EPSILON 属性的值接近于2.22044604925031308088472633361816e-16
function equal(a,b){
if(Math.abs(a-b)<Number.EPSILON){
return true;
}else{
return false;
}
}
console.log(0.1+0.2===0.3); //打印false
console.log(equal(0.1+0.2,0.3)); //打印true
//1. 二进制和八进制
let b=0b1010;
let o=0o777;
console.log(b); //打印10
console.log(o); //打印511
let d=100;
let x=0xff;
console.log(d); //打印100
console.log(x); //打印255
//2.Number.isInteger 判断一个数是否为有限数
console.log(Number.isFinite(100)); //打印true
console.log(Number.isFinite(100/0)); //打印false
console.log(Number.isFinite(Infinity)); //打印false
//3.Number.isNaN 检测一个数值是否为NaN
console.log(Number.isNaN(123)); //打印false
//4. Number.parseInt Number.parseFloat 字符串转整数
console.log(Number.parseInt(5)); //打印5
console.log(Number.parseFloat ('3.1415926神奇')); //打印3.1415926
//5.Number.isInteger 判断一个数是否为整数
console.log(Number.isInteger(5)); //打印true
console.log(Number.isInteger(2.5)); //打印false
//6.Math.trunc 将数字的小数部分抹掉
console.log(Math.trunc(3.5)) //打印3
//7.Math.sign 判断一个数到底为正数 负数 还是零
console.log(Math.sign(100)); //打印1
console.log(Math.sign(-20)); //打印-1
console.log(Math.sign(0)); //打印0
</script>
</body>
</html>