# Number和Math

# Number

Number(xx) // 把某个值转化为数字类型

num.toFixed(x) // 把数字转换为字符串,返回指定x位小数点数字

Number.parseInt() // 转换为整数 
Number.parseInt(1e1) // 10

Number.parseFloat() // 转换为浮点数

Number.isFinite(); // 检查一个数值是否为有限的(finite)

Number.isNaN() // 检查一个值是否为NaN

Number.isInteger() // 判断一个数值是否为整数 如果不是数值返回false。

# Math

常量
Math.PI // 返回圆周率
Math.E          // 返回欧拉指数(Euler's number)
Math.PI         // 返回圆周率(PI)
Math.SQRT2      // 返回 2 的平方根
Math.SQRT1_2    // 返回 1/2 的平方根
Math.LN2        // 返回 2 的自然对数
Math.LN10       // 返回 10 的自然对数
Math.LOG2E      // 返回以 2 为底的 e 的对数(约等于 1.414)
Math.LOG10E     // 返回以 10 为底的 e 的对数(约等于0.434)

方法
Math.abs()  // 返回绝对值

Math.max()/Math.min() // 返回最大/最小值

Math.random() // 返回0~1随机数

Math.round() // 返回四舍五入整数

Math.floor() // 返回向下取整

Math.ceil() // 返回向上取整

Math.pow(x, y) // 返回 x 的 y 次幂

Math.sqrt() // 返回平方根

Math.cbrt() // 返回立方根

Math.trunc() // 去除小数部分,返回整数部分, 如果不是数值返回NaN

Math.sign() // 判断一个数到底是正数、负数、还是零。非数值,会先转换为数值
// 为正数,返回+1;
// 为负数,返回-1;
// 为 0,返回0;
// 为-0,返回-0;
// 其他值,返回NaN。

Math.hypot() // 返回所有参数的平方和的平方根

Math.sin() // 返回角 x(以弧度计)的正弦
Math.cos() // 返回角 x(以弧度计)的余弦
Math.tan(() // 返回角 x(以弧度计)的正切
Math.asin() // 返回角 x(以弧度计)的反正弦
Math.acos() // 返回角 x(以弧度计)的反余弦
Math.atan() // 返回角 x(以弧度计)的反正切

Math.sinh(x) // 返回x的双曲正弦(hyperbolic sine)
Math.cosh(x) // 返回x的双曲余弦(hyperbolic cosine)
Math.tanh(x) // 返回x的双曲正切(hyperbolic tangent)
Math.asinh(x) // 返回x的反双曲正弦(inverse hyperbolic sine)
Math.acosh(x) // 返回x的反双曲余弦(inverse hyperbolic cosine)
Math.atanh(x) // 返回x的反双曲正切(inverse hyperbolic tangent)

# Number.isNaN() 和 isNaN()的区别

isNaN() 检测一个值是不是不是number,false为数字,true为NaN不是数字。

Number.isNaN() 检测是不是NaN值。 NaN值为true 否则都为false。

const name = "Lydia Hallie";
const age = 21;

console.log(Number.isNaN(name)); // false
console.log(Number.isNaN(age)); // false

console.log(isNaN(name)); // true
console.log(isNaN(age)); // false

求数组最大值

Math.max.apply([], arr)

Math.max(...arr)

求某个区间随机数

(max - min) * Math.random() + min

Math.ceil(80 * Math.random() + 20) // 返回 20 ~ 100 的随机整数

Math.floor(100 * Math.random()) 100  // 以内随机整数